1. function internal attribute arguments
Arguments is used to save the function parameters. arguments. callee points to a function with an arguments object.
Copy codeThe Code is as follows:
// Factorial
Function factorial (num ){
If (num <= 1 ){
Return 1;
} Else {
Return num * arguments. callee (num-1); // use agreements. callee instead
}
}
Var trueFactorial = factorial;
Factorial = function {
Return 0;
}
Alert (trueFactorial (5); // 20
Alert (factorial (5); // 0
2. Function Attributes and Methods
Length attribute, indicating the number of function parameters
3. apply () and call () Methods
The apply () and call () methods are used to pass parameters or expand the function scope.
Copy codeThe Code is as follows:
// PASS Parameters
Function sum (num1, num2 ){
Return num1 + num2;
}
Function callSum (num1, num2 ){
Return sum. call (this, num1, num2); // The first parameter this, which is listed later
}
Alert (callSum (10, 10); // 20
Function calSum1 (num1, num2 ){
Return sum. apply (this, arguments); // The first parameter this, the second parameter arguments
}
Function calSum2 (num1, num2 ){
Return sum. apply (this, [num1, num2]); // The first parameter this, and the second parameter is the parameter Array
}
Alert (callSum1 (10, 10); // 20
Alert (callSum2 (10, 10); // 20
Copy codeThe Code is as follows:
// Change the function Scope
Window. color = "red ";
Var o = {color: "blue "};
Function sayColor (){
Alert (this. color );
}
SayColor (); // red
SayColor. call (this); // red
SayColor. call (window); // red
SayColor. call (o); // blue