1. Function Internal Property arguments
Arguments is used to hold the parameters of the function, Arguments.callee point to the function that owns the arguments object
Copy Code code as follows:
Factorial
function factorial (num) {
if (num <= 1) {
return 1;
} else {
Return Num*arguments.callee (num-1); Replace with Agreements.callee
}
}
var truefactorial = factorial;
factorial = function {
return 0;
}
Alert (Truefactorial (5)); 20
Alert (factorial (5)); 0
2. Properties and methods of functions
Length property, representing the number of function arguments
3. Apply () and call () method
The Apply () and call () methods are used to pass the scope of an argument or extension function
Copy Code code as follows:
Passing parameters
function sum (num1,num2) {
return num1+num2;
}
function Callsum (num1,num2) {
Return Sum.call (THIS,NUM1,NUM2); The first argument, this, lists all the parameters
}
Alert (Callsum (10,10)); 20
function CalSum1 (num1,num2) {
Return sum.apply (this,arguments); First argument this, second argument arguments
}
function calSum2 (num1,num2) {
Return sum.apply (this,[num1,num2]); The first argument is this, and the second argument is an array of arguments
}
Alert (CALLSUM1 (10,10)); 20
Alert (callSum2 (10,10)); 20
Copy Code code as follows:
Changing the scope of a function
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