arguments, function extension operator, parameter default value, parameter and return value, variable
Function arguments
-
Because console.log(); It is also called through (), so log is a function
-
Characteristics of log function
One or more parameters can be received
console.log(1);//1 console.log(1,2,3);//1,2,3 console.log(1,2,3,4);//1,2,3,4
3. Can the log function receive one or more functions?
The internal implementation principle of the log function uses arguments
4. The role of arguments?
Used to hold all arguments passed to the function.
function number(){ //Note: there is something called arguments inside each function. //arguments is actually a pseudo array. console.log(arguments); //By array console.log(arguments[0]);//5 console.log(arguments[1]);//10 } number(5,10);
arguments in function
function num(){ let sum=0; for(let i=0; i<arguments.length;i++){ let res=arguments[i]; console.log(res);//2 4 6 //sum = 0 + 2 ; sum = 2 ; //sum = 2 + 4 ; sum = 6 ; //sum = 6 + 6 ; sum = 12 ; sum += res; // sum = sum + res ; } //Return value return sum; } let msg = num (2,4,6); console.log(msg); // 12
Function extension operator
Function extension operators can be written to the left and right of the equal sign (assignment operator).
1. When the extension operator is to the left of the equal sign, it packages the remaining data into a new array.
let [a,...b] = [1,2,3]; //a=1; //b=[2,3];
Note: extension operators can only be written at the end. (it cannot be written in the front and middle position)
2. When the extension operator is on the right of the equal sign, it is to untie the data in the array.
let arr1=[1,3,5]; let arr2=[2,4,6]; let arr3 = [...arr1,...arr2]; // let arr3 = [1,3,5,2,4,6];
-
The role of extension operators in the formal parameter list of functions.
Package all arguments passed to the function into an array.
Note: like the extension operator on the left, it can only be written at the end of the formal parameter list.
function number(s,...values){ console.log(s);//5 console.log(values);//15 } number(5,15);
The extension operator is written at the end of the formal parameter list to receive all the remaining data
The results obtained are as like as two peas obtained by arguments (the same result is found in different ways).
function num(...values){ let sum=0; for(let i=0; i<values.length;i++){ let res=values[i]; sum += res; } //Return value return sum; } let msg = num (5,6); console.log(msg); // 11
Default value of function parameter
1. Before ES6, you can specify default values for formal parameters through logical operators.
The format is: condition A | condition B
If condition A holds, condition A is returned
If condition A is not true, condition B will be returned whether condition B is true or not
Starting from ES6, you can specify the default value directly after the formal parameter through =.
function number(a="Be happy",b="Happy"){ console.log(a,b); } number();
Note: the default value from ES6 can also be obtained from other functions.
function number(a="Be happy",b=num()){ console.log(a,b); } number(); function num(){ return "safe and peaceful" }
Function as parameter and return value
In JavaScript, functions can be used as parameters.
Note: functions can be nested in JavaScript, but functions cannot be nested in other programming languages.
//Use the function as the return value of other functions function res(){ let msg=function(){ console.log(123); } return msg; } let fn=res();// let fn=res; fn();
JavaScript variables
How to modify the data stored in variables
We just need to assign a direct value to the variable again
Simply define a variable
let get;
We can store data into variables
get = 666;
We can take the stored data from the variable
console.log(get); // 666
What is variable initialization?
The first assignment to a variable in JavaScript is called "variable initialization".
-
How to modify the data stored in variables
In JavaScript, if we want to modify the data stored in the variable, we just need to assign a value to the variable directly again.
get = 888; console.log(get); // 888
2. The first assignment to a variable in JavaScript is called "variable initialization"
let res; res = 555; // Initialization of variables res = 999; // Is not the initialization of a variable
3. If a variable we declare is not initialized, what is the data stored in it?
In JavaScript, if we define a variable but do not initialize it, the data stored in the variable is undefined.
let msg; console.log(msg) ; // undefined
4. We can initialize variables in many forms
-
You can define variables before initializing them
let num ; num 333 ;
-
You can also initialize variables when defining variables
let num1 = 123 ;
5. Other formats for defining variables
We can define a single variable, or two or three variables at the same time
The format of defining multiple variables at the same time is:
let variable name 1, variable name 2;
let res,res1; // Define two variables at the same time let msg,msg1,msg2; // Define three variables at the same time
6. Other formats of initialization variables
Note: if the initialization values of multiple variables are the same in enterprise development, we can
Variable name 1 = variable name 2 = variable name 3... = initialization value;
(there are some disadvantages. If it needs to be changed later, there are too many processes.)
let num ; let num1 ; // Format 1: num = 111; num1 = 333 ; // Format 2: num = num1 = 666; console.log(num) ; // 666 console.log(num1) ; // 666
7. Initialize multiple variables while defining multiple variables
// Mode 1: let res ; let res1 ; res =123; res1 = 321; // Mode 2: (optimization) let num = 567, num1 = 369 ; console.log(num) ; // 567 console.log(num1) ; 369
-