One, two special objects inside the function: 1 arguments (Array object) 2 this
function sum (num) {//factorial, recursive
Return Num*sum (num-1);
}
SUM (4);--24
function sum (num) {
Return Num*arguments.callee (num-1);
}
SUM (4)--24
Arguments.callee (); Call itself to implement function recursion
Callee is a property of the arguments object, pointing to the function that owns this arguments object.
Second, window is an object, is the largest object in JS, is the outermost object, representing the global.
var color= "Red"; ==window.color= "Red"; ==this.color= "Red";
1 Global variables are properties under the Window object
2 in the global scope, this refers to the Window object.
var box={
Color: "Blue",//The color here is the box under the property, is the local variable box.color= "blue";
Saycolor:function () {
alert (This.color); Here's this means the box object
}
}
function is an object, so there are its own properties and methods, each function contains two properties: Length and prototype
1 Length: The number of arguments to the function
function box (name,age) {
return name+age;
}
Alert (box.length),---2 means that this function has two parameters
2 prototype under the two methods: Apply and call, using them, you can impersonate another function to execute.
function box (num1,num2) {
return num1+num2;
};
Alert (Box (10,10)); ----20
function sum (num1,num2) {
Return box.apply (this,[num1,num2]);
This refers to the Window object, because the box is under the window scope, the [] array is the passed parameter, or the arguments is used instead of [num1,num2].
}
Alert (sum (10,10));----20
function sum (num1,num2) {
Return Box.call (THIS,NUM1,NUM2)
The difference between call and apply is that the pass parameter is different, and apply is an array form, and call is a parameter of the argument.
}
Use:
var color= "Red"; The Global
var box={
Color: "Blue"//Local
}
function SayHello () {
alert (This.color);
}
Alert (SayHello ());---red because the SayHello function is under the window global scope, so this is also the window.
If you use call, you can implement an impersonation of an object:
Sayhello.call (window) ==sayhello.call (this)----the Red scope under window
Sayhello.call (box))----The Blue scope under box
Coupling: interrelated
Two special objects in a function