Implicit parameters of JS (arguments, callee, caller)

Source: Internet
Author: User

Address: http://www.archlinuxchina.com/forum/thread-59-1-1.html

Welcome to the Web technology exchange group: 220851151

Before talking about the above concepts, we should first talk about the implicit parameters of functions in javascript:


Arguments

Arguments this object represents the function being executed and the parameters that call its function. [Function.] arguments [N] Parameter

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. It indicates that arguments is a hidden object created in addition to the specified parameters when a function is called. 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, you can use arguments [N] to access the values of a single parameter and have the Length attribute of the array length. In addition, the arguments object stores the parameters actually passed to the function, not limited to the list of parameters defined in the function declaration, and cannot explicitly create the arguments object. The arguments object is available only when the function starts. The following example details these properties: // arguments
Object usage.

Function argtest (a, B ){

VaR I, S = "The argtest function expected ";

VaR numargs = arguments. length; // obtain the value of the passed parameter.

VaR expargs = argtest. length; // obtain the value of the expected parameter.

If (expargs <2)

S + = expargs + "argument .";

Else

S + = expargs + "arguments .";

If (numargs <2)

S + = numargs + "was passed .";

Else

S + = numargs + "were passed .";

S + = ""

For (I = 0; I <numargs; I ++) {// obtain the parameter content.

S + = "Arg" + I + "=" + arguments + "";

}

Return (s); // return the parameter list.

}

A code indicating that arguments is not an array (array class) is added here: array. Prototype. selfvalue = 1;

Alert (new array (). selfvalue );

Function testaguments (){

Alert (arguments. selfvalue );

}

Run the code and you will find that the first alert shows 1, which indicates that the array object has the selfvalue attribute and the value is 1. When you call the testaguments function, "undefined" is displayed, indicating that it is not an attribute of arguments, that is, arguments is not an array object.

Here, we recommend a simple method: Alert (arguments instanceof array );

Alert (arguments instanceof object );


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 is the same as functionname. tostring, that is, the decompilation Text of the function is displayed,
Note: function. tostring () can implement the function decompilation function. If the recursive function is added, the function is more powerful.
The following example illustrates the usage of caller attributes: // caller demo {
Function callerdemo (){
If (callerdemo. Caller ){
VaR A = callerdemo. Caller. tostring ();
Alert ();
} Else {
Alert ("this is a top function ");
}
}
Function handlecaller (){
Callerdemo ();
}


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. 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, which facilitates anonymity.

Recursion of a function or encapsulation of a function. For example, the following example recursively calculates the sum of natural numbers from 1 to n. This attribute

It is available only when the related function is being executed. Note that callee has the Length attribute, which is sometimes

It is better for verification. Arguments. length is the length of the real parameter, and arguments. callee. length is
The length of the parameter to determine whether the length of the parameter is consistent with that of the actual parameter. 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 );

}

}

// Recursive Calculation

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 );

}

Call time: Alert (sum (100 ));

The function contains a reference to sum itself. The function name is only a variable name. Calling sum inside the function is equivalent to calling

A global variable cannot reflect the call itself. Using callee is a good method.


Apply and call

They are used to bind a function to another object for running. The two are different only when defining parameters:
Apply (thisarg, argarray );

Call (thisarg [, arg1, arg2…] ]);

That is, the this pointer inside all functions will be assigned to thisarg, this can implement the purpose of running the function as another object method. The description of apply will cause a typeerror if argarray is not a valid array or arguments object.

If neither argarray nor thisarg is provided, the global object will be used as thisarg,

And no parameters can be passed. 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: the application call and apply has another technique in it, 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 ". Take the following example: // an example 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 example above shows that after calling, extend can inherit the methods and attributes of the base. By the way, in the Javascript framework prototype, use apply to create a mode for defining classes. The implementation code is as follows: var class = {

Create: function (){

Return function (){

This. Initialize. Apply (this, arguments );

}

}

} Parsing: From the code perspective, this object only contains one method: Create, which returns a function, that is, a class. But this is also a class

The constructor calls initialize, which is the initialization function defined during class creation. In this way,

The following is an example of the class Creation Mode in prototype: var vehicle = Class. Create ();

Vehicle. Prototype = {

Initialize: function (type ){

This. type = type;

}

Showself: function (){

Alert ("this vehicle is" + this. type );

}

} Var Moto = new vehicle ("Moto ");

Moto. showself ();

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.