1. The internal attribute arguments
arguments of the function is used to save the parameters of the function. arguments.callee points to the function owning the arguments object
//Factorial
function factorial(num) {
if (num <= 1) {
return 1;
} else {
return num*arguments.callee(num-1); //Replace
} with agreements.callee
}
var trueFactory = factorial;
factorial = function {
return 0;
}
alert(trueFactorial(5)); //20
alert(factorial(5)); //0
2. Function attributes and method
length attribute, indicating the number of function parameters
3. apply() and call() methods
apply() and call() methods are used to pass parameters or expand functions Scope
//Pass parameters
function sum( num1,num2) {
return num1 num2;
}
function callSum(num1,num2) {
return sum.call(this,num1,num2); //The first parameter this, List all parameters 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]); //First parameter this, the second parameter is the parameter array
}
alert(callSum1(10,10)); //20
alert(callSum2(10,10)); //20
//Change 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