当前位置:网站首页>JS notes (III)
JS notes (III)
2022-07-03 21:22:00 【Deduct 1 to send hell fire】
Catalog
2.2 The parameters of the function
2.3 The return value of the function
2.5 Two ways to declare functions
1. Array
1.1 Create array
JavaScript There are two ways to create arrays in :
- utilize new Create array
- Create an array using array literals
utilize new Create array
var Array name = new Array(); // Create a new empty array
Create an array using array literals
// 1. Create an empty array using array literal
var Array name =[];
// 2. Create an array with an initial value using array literal
var Array name =['a','b','b'];
// 3. Array can store any type of data , Like strings , Numbers , Boolean value, etc
var arrStus =['haha',6,true,6.1,'a'];
- The literal of the array is square brackets
[] - Declaring an array and assigning values is called array initialization
1.2 Index of array
Indexes : The ordinal number used to access an array element ( Array index from 0 Start )
// Define an array
var arr = [1,2,3];
// Gets the number... In the array 3 Elements
alert(arr[2]);
1.3 Traversal array
adopt for The circular index traverses each item in the array
// for Loop through groups
var arr = ['a','b', 'c'];
for (var i = 0; i < arr.length; i++){
console.log(arr[i]);
}
1.4 Length of array
Use “ Array name .length” You can return the number of array elements
var arr = [1,2,3];
alert(arr.length); // Output 3
length Attributes will dynamically change with the number of elements in the array
1.5 Add array elements
1 It can be modified by length Length to achieve the purpose of array expansion
var arr = ['a','b','c'];
arr.length =5;
console.log(arr);
The index number is 3 4 Array element of did not give a value , Is to declare that the variable is not given a value , The default value is undefined
2 Add new array elements by modifying the array index
var arr = ['a','b','c'];
arr[3]='d';
console.log(arr);// Output a b c d
1.6 Flip array
var arr=['a','b','c'];
var new_Arr=[];
for(var i=arr.length-1;i>=0;i--){
new_Arr[new_Arr.length]=arr[i];
}
console.log(new_Arr);1.7 Bubble sort
var arr=[1,2,6,5,19,4];
for(var i=0;i<arr.length;i++){
for(var j=0;j<=arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
var mid=arr[j];
arr[j]=arr[j+1];
arr[j+1]=mid;
}
}
}
console.log(arr);2. function
function : It encapsulates a section of A block of code that can be repeatedly called and executed . A lot of code can be reused through this code block .
2.1 Use of functions
The function is used in two steps : Declare functions and Call function
1 Declare functions
function Function name (){
// Function body code
}
2 Call function
function Function name (){
// Function body code
}
Function name (); Execute the function body code by calling the function name Be careful : Declaring the function itself does not execute code , Function body code is executed only when the function is called .
2.2 The parameters of the function
When you declare a function , You can add some parameters in parentheses after the function name , These parameters are called Shape parameter , And in the Call this function when , You also need to pass the corresponding parameters , These parameters are called Actual parameters .
Function of parameters : stay Internal function Some values cannot be fixed , We can use parameters in Pass different values when calling a function go in
function Function name ( Shape parameter 1, Shape parameter 2 , Shape parameter 3...) { // You can define as many parameters as you want , Separate with commas
// The body of the function
}
Function name ( Actual parameters 1, Actual parameters 2, Actual parameters 3...);
example Addition of two numbers
function sum(a,b){
alert(a+b);
}
sum(1,2);Be careful : stay JavaScript in , The default value of a parameter is undefined
2.3 The return value of the function
sometimes , We would want the function to return the value to the caller , At this time, by using return Statement can be implemented .
return The syntax format of the statement is as follows :
// Declare functions
function Function name (){
...
return The value to be returned ;
}
// Call function
Function name (); // At this point, you can call the function to get the function body return Value after
In the use of return When the sentence is , Function will stop executing , And returns the specified value
If the function does not return , The value returned is undefined
- return The code after the statement is not executed
- return Only one value can be returned . If you separate multiple values with commas , Subject to the last one
Function calls another function
Because each function is a separate block of code , Used to complete special tasks , Therefore, it is often used when functions call each other .
2.4 arguments Use
When we are not sure how many parameters are passed , It can be used arguments To get . stay JavaScript in ,arguments In fact, it is a built-in object of the current function . All functions have a built-in arguments object ,arguments Object stores all the arguments passed .
arguments It stores the passed arguments
arguments The display form is a pseudo array , So you can traverse . Pseudo arrays have the following characteristics
- have length attribute
- Store data by index
- Without array push , pop Other methods
for example : Use the function to find the maximum value of any number
function maxNum() {
var max = arguments[0];
for (var i = 0; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i];
}
}
return max;
}
console.log(maxNum(2, 4, 5, 9)); // 9
console.log(maxNum(12, 4, 9)); // 12
2.5 Two ways to declare functions
1 Name the function
// Declaration definition method
function Function name () {...}
// call
Function name ();
2 Anonymous functions
// This is a function expression , Anonymous functions end with a semicolon
var Variable name = function(){...};
// The way it's called , The function call must be written below the function body
Variable name ();
Because the function has no name , So it's also called Anonymous functions
This Variable name Stored inside is a function
The code of the function call must be written after the function body
边栏推荐
- Sort out several network request methods of JS -- get rid of callback hell
- What if the Flink SQL client exits and the table is emptied?
- @Scenario of transactional annotation invalidation
- "Designer universe" argument: Data Optimization in the design field ultimately falls on cost, safety and health | chinabrand.com org
- JVM JNI and PVM pybind11 mass data transmission and optimization
- 浅析 Ref-NeRF
- Hcie security Day10: six experiments to understand VRRP and reliability
- Set, weakset, map, weakmap in ES6
- JS three families
- Preliminary understanding of C program design
猜你喜欢

Is it OK for fresh students to change careers to do software testing? The senior answered with his own experience
![Capturing and sorting out external articles -- autoresponder, composer, statistics [III]](/img/bf/ac3ba04c48e80b2d4f9c13894a4984.png)
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]

常用sql集合

Common SQL sets

No more! Technical team members resign collectively

Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.

Visiontransformer (I) -- embedded patched and word embedded

What is the maximum number of concurrent TCP connections for a server? 65535?

MySQL——索引
![[Yugong series] go teaching course 002 go language environment installation in July 2022](/img/47/35b4fb0354122e233977b261ef405b.png)
[Yugong series] go teaching course 002 go language environment installation in July 2022
随机推荐
Getting started with postman -- environment variables and global variables
The 12th Blue Bridge Cup
Nmap and masscan have their own advantages and disadvantages. The basic commands are often mixed to increase output
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
同花顺开户注册安全靠谱吗?有没有风险的?
Service discovery and load balancing mechanism -service
Qualcomm platform WiFi update disconnect end open event
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
C程序设计的初步认识
Etcd raft Based Consistency assurance
Scientific research document management Zotero
Etcd 基于Raft的一致性保证
Under the double reduction policy, research travel may become a big winner
不同业务场景该如何选择缓存的读写策略?
一台服务器最大并发 tcp 连接数多少?65535?
请教大家一个问题,用人用过flink sql的异步io关联MySQL中的维表吗?我按照官网设置了各种
Hcie security Day12: supplement the concept of packet filtering and security policy
Reinforcement learning - learning notes 1 | basic concepts
Goodbye 2021, how do programmers go to the top of the disdain chain?