Two special objects in a function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.