Detailed interpretation of function data types
A function is a method that can achieve a certain function
"Encapsulate" the code that implements a certain function and then want to realize this function later, without rewriting the code, only need to execute the function
Execution function => function (argument 1, argument 2, ...)
Actual parameter (specific value): It is the specific value passed to the line parameter variable of the function
For example, sum(17), x=17, y=undefined, the creation function defines line parameters, but no actual parameter value is passed during execution, and the default value is undefined;
Example sum(10,20,30),x=10,y=20,
例sum(),x=undefined,y=undefined,
Function exit: return value mechanism
function sum(x=0,y=0){
var total=x+y,
average=total/2;
a=average.toFixed(2);
return"@@";
}
sum : ƒ sum(x,y){} represents the function itself
sum(): Function execution represents the result returned after execution (see return)
var result=sum(10,20)
console.log(resule)
return
Do not write return or there is nothing after return, the default return value is undefined
What is behind the return is what is returned
return must be a value
As long as return is encountered in the function body, the code below return will not be executed again!
function sum(x=0,y=0){
var total=x+y,
average=total/2;
a=average.toFixed(2);
return"ABC";
}
var aa=sum();
console.log(aa) //ABC
anonymous function (no function name)
Function expression: use the created function as a value, assign it to a variable or other content (operations that use a function as a value are called function expressions)
Self-executing function: the creation function and the execution function are completed together (after the function is created, the function is executed immediately)
The function created is stored in the first parentheses
The second parenthesis is to execute the function
Example (function(x){
})(100;)
Wrapped in parentheses, just for syntax support
In addition to wrapping the function in parentheses to solve the problem of grammatical errors, add ~±! can also make the syntax correct
function (x){
}(100)
Normal creation: declare a variable called fn, but the stored value is a function
function fn(){}
"Function expression" creates a function: the effect is the same as the above method (there are some differences when variables are promoted)
var fn=function(){
This inside is also equivalent to a function expression, creating an anonymous function and returning it as a value
return function(){}
}
var f=fn();
document.body.οnclick=function(){}
Requirement: When executing the function, the actual parameter value is passed, but how many actual parameters are passed is "uncertain", and we want to accept the actual parameter information passed by the function
Set formal parameter variables, but you need to know the number and order of passing actual parameters
Function built-in actual parameter set arguments: Regardless of whether or not to pass and how many actual parameters are passed (regardless of whether formal parameters are set), the collection contains all the passed in actual parameter information
Not passing is an empty collection
This collection is an "array-like"
The "..." remaining operator in ES6: The remaining operator in the function parameter can obtain the actual parameter information received by the previously set formal parameter variable, and the remaining actual parameters are placed in this set
If none of the formal parameter variables are defined, all passed actual parameter information is stored in this collection
This collection is an "array" collection
function fn(){
console.log(arguments);
}
fn();
fn(10);
fn(10,20);
fn(10,20,30)
params variable, store actual parameter set, array
function fn(x,…params){
console.log(params,arguments) can be used with arguments at the same time
}
fn()
fn(10)
fn(10,20)
The most common method of fn(10,20,30)
to sum any array is the arguments method