Details about caller callee call apply in javascript

Source: Internet
Author: User

Before talking about the above concepts, we should first talk about the implicit parameter of the function in javascript: arguments

Arguments
This object represents the parameters of the runtime function.
[Function.] arguments [n]
Parameters
Function: option. Name of the Function object currently being executed.
N: option. The index of the parameter value starting from 0 to be passed to the Function object.
Description
Arguments is a hidden object created in addition to the specified parameters during function execution. Arguments is an object similar to an array but not an array. It is similar to an array because
It has the same access nature and method as an array. arguments [n] can be used to access the value of a single parameter and has the length attribute of the array length. There is
The arguments object stores the parameters actually passed to the function, not limited to the list of parameters defined by the function declaration, and cannot explicitly create arguments
Object. The arguments object is only available when the function is running. The following example details these properties:
// Usage of the arguments object.
Function ArgTest (a, B ){
Var I, s = "The ArgTest function expected ";
Var numargs = arguments. length; // obtain the value of the passed parameter. (Real parameter)
Var expargs = ArgTest. length; // obtain the value of the expected parameter. (Row parameter)
If (expargs <2)
S + = expargs + "argument .";
Else
S + = expargs + "arguments .";
If (numargs <2)
S + = numargs + "was passed .";
Else
S + = numargs + "were passed .";
S + = "\ n"
For (I = 0; I <numargs; I ++) {// obtain the parameter content.
S + = "Arg" + I + "=" + arguments [I] + "\ n ";
}
Return (s); // return the parameter list.
}
A code indicating that arguments is not an Array (Array class) is added here:
Alert (arguments instanceof Array); // false
Alert (arguments instanceof Object); // true
Caller
Returns a reference to the function that calls the current function.
FunctionName. caller
The functionName object is the name of the executed function.
Description
For a function, the caller attribute is defined only when the function is executed. If the function is called by the top layer, caller contains null.
. If the caller attribute is used in the string context, the result and functionName. toString
That is to say, The Decompilation Text of the function is displayed.
The following example illustrates the usage of caller attributes:
// Caller demo {
Function callerDemo (){
If (callerDemo. caller ){
Var a = callerDemo. caller. toString ();
Alert ();
// The name Of The function that will call this function. Note that caller. name is not compatible with ie.
Var callerFun = callerDemo. caller. name
Alert (callerFun );
} Else {
Alert ("this is a top function ");
}
}
Function handleCaller (){
CallerDemo ();
}
HandleCaller ();
Callee
Returns the Function object being executed, that is, the body of the specified Function object.
[Function.] The arguments. callee option function parameter is the name of the currently executed Function object.
Description
The initial value of the callee attribute is the Function object being executed.
The callee attribute is a member of the arguments object. It indicates a reference to the function object itself. This facilitates recursion of anonymous functions or ensures function encapsulation. For example, the recursive program in the following example
Calculate the sum of natural numbers from 1 to n. This attribute is only available when the related function is being executed. Note that callee has the length attribute, which is sometimes used for verification.
. Arguments. length is the length of the real parameter, and arguments. callee. length is the length of the form parameter, which can determine whether the length of the form parameter is the length of the real parameter.
Consistency.
Example
// Callee can print itself
Function calleeDemo (){
Alert (arguments. callee );
}
// Used to verify Parameters
Function calleeLengthDemo (arg1, arg2 ){
If (arguments. length = arguments. callee. length ){
Window. alert ("verify that the length of the form parameter and real parameter is correct! ");
Return;
} Else {
Alert ("real parameter length:" + arguments. length );
Alert ("parameter length:" + arguments. callee. length );
}
}
// Uses callee's recursive Calculation
Var sum = function (n ){
If (n <= 0)
Return 1;
Else
Return n + arguments. callee (n-1 );
}
// Typical recursive functions:
Var sum = function (n ){
If (1 = n) return 1;
Else return n + sum (n-1 );
Call time: alert (sum (100). The function contains a reference to sum itself. The function name is just a variable name, calling sum in a function is equivalent to calling a global variable. It cannot reflect itself. Using callee is a better method.
Call time:
They are used to bind a function to another object for running. The two are different only when defining parameters:

Apply (thisArg, argArray); // The second parameter is an array or arguments.

Call (thisArg [, arg1, arg2…]); // Single parameter

That is, the this pointer inside all functions will be assigned to thisArg, which can be used to run functions as methods of another object.
Description of apply
If argArray is not a valid array or an arguments object, a TypeError occurs. If neither argArray nor thisArg is provided, the Global object will be used as thisArg and cannot be passed with any parameters.
Call description
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 provided, the Global object is used as the thisArg
Related skills:
There is another technique in applying call and apply, that is, after applying call and apply to another function (class), the current
A function (class) has another function (class) method or attribute, which can also be called "inheritance ". See the following example:
// Inherited demo
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 example above shows that after calling, extend can inherit the methods and attributes of the base.

--------------------------- Split line ---------------------------------

Yesterday I read a blog post about js writing methods, including call and callee. I don't know about this. I found a good article in the blog Park. I have made some corrections and typographical work. The original Article is here.

Related Article

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.