Understand the arguments,callee,caller,apply and call__javascript in JavaScript

Source: Internet
Author: User
First, Arguments
The object represents the function being executed and the arguments of the function that called it.
[function.] Arguments[n]
Parameter function: option. The name of the Function object that is currently executing.
N: Options. The 0-based parameter value index to pass to the Function object.
Description: Arguments is a hidden object that is created in addition to the parameters specified when a function call is made.
Arguments is an array-like but not an array object, saying that he is similar to an array because it has the same access to the array and the way it can be accessed by arguments[n] to the value of the corresponding individual parameter and has the array length attribute length. And the arguments object stores the arguments that are actually passed to the function, not the list of parameters defined by the function declaration, and the arguments object cannot be explicitly created.
This adds a code stating that arguments is not an array (array Class):
Array.prototype.selfvalue = 1;
Alert (new Array (). Selfvalue);
function testaguments () {
alert (Arguments.selfvalue);
}
Run code you will find that the first alert shows 1, which means that the array object has the Selfvalue property, the value is 1, and when you call the function testaguments, you will notice that "undefined" is displayed, stating that the attribute is not arguments. That is, arguments is not an array object.

Second, caller
Returns a reference to a function that calls the current function.
Functionname.caller
The FunctionName object is the name of the function being executed.
Note: For functions, the caller property is defined only when the function executes. If the function is called by the top level, then caller contains null. If the caller property is used in the string context, the result is the same as the functionname.tostring, that is, the inverse compiled text of the function is displayed.
The following example illustrates the use of the caller property:
function Callerdemo () {
if (Callerdemo.caller) {
var a= callerDemo.caller.toString ();
alert (a);
} else{
Alert ("This are a top function");
}
}
function Handlecaller () {
Callerdemo ();
}
Third, callee
Returns the function object being executed, which is the body of the specified function object.
[function.] Arguments.callee
The optional function parameter is the name of the function object that is currently executing.
Description: The initial value of the Callee property is the Function object that is being executed.
The Callee property is a member of a arguments object that represents a reference to the function object itself, which facilitates the recursion of anonymous functions or ensures the encapsulation of functions, such as the sum of the natural numbers of 1 to n from the following example's recursive computation. This property is available only if the related function is executing. More to note is that callee has the length attribute, which is sometimes used for validation or better. The arguments.length is the real parameter, and the arguments.callee.length is the parameter length, thus it is possible to judge whether the parameter length of the call is consistent with the actual parameter length.

Callee can print its own
function Calleedemo () {
alert (Arguments.callee);
}
Recursive computation
var sum = function (n) {
if (n <= 0) return 1;
else return n +arguments.callee (n-1)
}
A more general recursive function:
var sum = function (n) {
if (1==n) return 1;
else return n + sum (n-1);
}
When invoked: Alert (SUM (100));
Where the function contains a reference to sum itself, the function name is only a variable name, within the function call sum is equivalent to call a global variable, not very good to reflect the call itself, then use callee would be a better way.

Iv. Apply and call
Their role is to bind the function to another object to run, the two only in the definition of the parameters of the way to distinguish:
Apply (Thisarg,argarray);
Call (Thisarg[,arg1,arg2 ...]);
That is, the this pointer within any function is assigned to THISARG, which enables the function to run as a method of another object
Apply Description: If Argarray is not a valid array or is not a arguments object, it will cause a typeerror.
If you do not provide any of the Argarray and Thisarg parameters, the Global object will be used as a thisarg and cannot be passed any parameters.
Description of Call: The call method can change the object context of a function from the initial context to the new object specified by Thisarg.
If the Thisarg parameter is not supplied, the global object is used as a thisarg
Related tips: Apply call and apply there is a trick in the inside, is to use call and apply another function (class), the current
A function (class) has a method or attribute of another function (class), which can also be referred to as "inheritance." Look at the following example:

Demo of Inheritance
function Base () {
This.member = "Dnnsun_member";
This.method = function () {
Window.alert (This.member);
}
}
function Extend () {
Base.call (this);
Window.alert (member);
Window.alert (This.method);
}
The above example shows that extend can inherit the methods and properties of base after call.
Incidentally, use apply to create a schema that defines a class in the JavaScript framework prototype.
The implementation code is as follows:
var Class = {
Create:function () {
return function () {
This.initialize.apply (this, arguments);
}
}
}
Parsing: From the code perspective, the object contains only one method: Create, which returns a function, that is, a class. But this is also the constructor of the class, which invokes initialize, which is the initialization function that is defined when the class is created. In this way, the class creation pattern in prototype can be implemented

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.