Fully Understand Javascript caller and callee concepts

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 function being executed and the parameters of the function that calls it.

[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.

Description

Arguments is a hidden object created in addition to the specified parameters when calling a function. 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:

 
 
  1. Viewplaincopytoclipboardprint?
  2. // Usage of the arguments object.
  3. FunctionArgTest (a, B ){
  4. Vari,S="TheArgTestfunctionexpected";
  5. Varnumargs=Arguments. Length; // obtain the value of the passed parameter.
  6. Varexpargs=ArgTest. Length; // obtain the expected parameter value.
  7. If (expargs<2)
  8. S + = expargs + "argument .";
  9. Else
  10. S + = expargs + "arguments .";
  11. If (numargs<2)
  12. S + = numargs + "waspassed .";
  13. Else
  14. S + = numargs + "werepassed .";
  15. S + = "\ n"
  16. For (I=0; Is + = "Arg" + I + "=" + arguments [I] + "\ n ";
  17. }
  18. Return (s); // return the parameter list.
  19. }

A code indicating that arguments is not an Array (Array class) is added here:

 
 
  1. viewplaincopytoclipboardprint?  
  2. Array.prototype.selfvalue=1;  
  3. alert(newArray().selfvalue);  
  4. functiontestAguments(){  
  5. 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.

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.
The following example illustrates the usage of caller attributes:

 
 
  1. viewplaincopytoclipboardprint?  
  2. //callerdemo{  
  3. functioncallerDemo(){  
  4. if(callerDemo.caller){  
  5. vara=callerDemo.caller.toString();  
  6. alert(a);  
  7. }else{  
  8. alert("thisisatopfunction");  
  9. }  
  10. }  
  11. functionhandleCaller(){  
  12. callerDemo();  

Callee

Returns the Function object being executed, that is, the body of the specified Function object.

[Function.] arguments. callee

The optional 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, 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

 
 
  1. Viewplaincopytoclipboardprint?
  2. // Callee can print itself
  3. FunctioncalleeDemo (){
  4. Alert (arguments. callee );
  5. }
  6. // Used to verify Parameters
  7. FunctioncalleeLengthDemo (arg1, arg2 ){
  8. If (Arguments. length= Arguments. callee. length ){
  9. Window. alert ("verify that the length of the form parameter and real parameter is correct! ");
  10. Return;
  11. } Else {
  12. Alert ("real parameter length:" + arguments. length );
  13. Alert ("parameter length:" + arguments. callee. length );
  14. }
  15. }
  16. // Recursive Calculation
  17. Varsum=Function(N ){
  18. If (n<= 0)
  19. Return1;
  20. Else
  21. Returnn + arguments. callee (n-1)
  22. }
  23. // Typical recursive functions:
  24. Varsum=Function(N ){
  25. If (1= N) return1;
  26. Elsereturnn + 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 within the function is equivalent to calling a global variable. It cannot reflect that it is the call itself, using callee is a good method.

Applyandcall

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, 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:

Another trick is to use call and apply to apply another function class .)
Function class) has another function class) method or attribute, which can also be called "inheritance ". See the following example:

 
 
  1. Viewplaincopytoclipboardprint?
  2. // Inherited demo
  3. Functionbase (){
  4. This. member="Dnnsun_Member";
  5. This. method=Function(){
  6. Window. alert (this. member );
  7. }
  8. }
  9. Functionextend (){
  10. Base. call (this );
  11. Window. alert (member );
  12. Window. alert (this. method );
  13. }

The example above shows that after calling, extend can inherit the methods and attributes of the base.

By the way, use apply in the prototype of the javascript framework to create a schema that defines classes,
The implementation code is as follows:

 
 
  1. viewplaincopytoclipboardprint?  
  2. varClass={  
  3. create:function(){  
  4. returnfunction(){  
  5. this.initialize.apply(this,arguments);  
  6. }  
  7. }  

Resolution: From the code, this object only contains one method: Create, which returns a function, that is, a class. But this is also a class constructor, in which initialize is called, and this method is the initialization function defined when the class is created. In this way, you can implement the class Creation Mode in prototype.

Example:

 
 
  1. viewplaincopytoclipboardprint?  
  2. varvehicle=Class.create();  
  3. vehicle.prototype={  
  4. initialize:function(type){  
  5. this.type=type;  
  6. }  
  7. showSelf:function(){  
  8. alert("thisvehicleis"+this.type);  
  9. }  
  10. }  
  11. varmoto=newvehicle("Moto");  
  12. moto.showSelf(); 

For more details about prototype, go to its official website.

  1. Cookie details in JavaScript
  2. Usage of confirm, alert, and prompt in JavaScript
  3. JavaScript-based REST client framework
  4. How to optimize the performance of JavaScript scripts
  5. How to connect to the Access database using Javascript

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.