Function: caller, arguments. callee, call, apply,

Source: Internet
Author: User
Caller

Returns the reference of the function that calls the current function!

The caller property is available only within the body of a function. If used outside a function declaration, the caller property is null.

If the currently executing function was invoked by the top level of a javascript program, the value of caller is null.

The this keyword does not refer to the currently executing function, so you must refer to functions and function objects by name, even within the function body.

The caller property is a reference to the calling function, so

 


  • If you use it in a string context, you get the result of calling functionname. tostring. That is, the decompiled canonical source form of the function.
  • You can also call the calling function, if you know what arguments it might want. thus, a called function can call its caller without knowing the name of the specified caller, provided it knows that all of its callers have the same form and fit, and that they will not call the called function again unconditionally (which wowould result in infinite recursion ).

 

 

Caller JavaScript code
1 function democaller (){
2 If (democaller. Caller ){
3 alert (democaller. Caller. tostring ());
4} else {
5 alert ("this is a top function ");
6}
7}
8 function callertest (){
9 democaller ();
10}

 

 

 

Caller HTML code
1 <button onclick = "democaller ()"> button 1 </button>
2 <button onclick = "callertest ()"> button 2 </button>

 

PS: Opera does not support caller and returns undefiend (isn't it BT? ) {Updated, new opera9.5 + supported}

 

 

 

Arguments. callee

Returning the currently running function body is awkward because the alert (arguments. callee) output is indeed a piece of the current function body.
(Actually, you can call the function itself to implement recursive calls to anonymous functions)

A property whose value is the function reference.

Arguments. calleeRefers to the function that is currently running. It provides a way for an unnamed function to refer to itself. This property is defined only within a function body.

Function (x ){
If (x <= 1) return 1;
Return x * arguments. callee (x-1 );
}

Callee JS Code
1 function democallee (a, B, c ){
2 alert ("real parameter length arguments. Length =" + arguments. Length) // 2
3 alert ("parameter length arguments. callee. Length =" + arguments. callee. Length) // 3
4 alert ("parameter length democallee. Length =" + democallee. Length) // 3
5 alert ("democallee === arguments. callee:" + (democallee = arguments. callee) // true
6}
7 democallee (1, 2)


Arguments. Length: actual parameter length
Arguments. callee. Length: parameter length
Democallee = arguments. callee: the two are equal.

 

Call, apply

Ecmascript specifies two methods that are defined for all functions,Call ()AndApply (). These methods allow you to invoke a function as if it were a method of some other object. The first argument to bothCall ()AndApply ()Is the object on which the function is to be invoked; this argument becomes the value ofThisKeyword within the body of the function. Any remaining argumentsCall ()Are the values that are passed to the function that is invoked.

Actually, it is to change the function pointer of the current function, similar to the object impersonating. The difference between the two methods is only in the first parameter.

Call JS Code
1 function classa (scolor ){
2 This. Color = scolor;
3 This. saycolor = function (){
4 alert (this. Color );
5 };
6}
7
8 function classb (scolor, sname ){
9 classa. Call (this, scolor );
10 This. Name = sname;
11 This. sayname = function (){
12 alert (this. Name );
13 };
14}
15
16 var obja = new classa ("red ");
17 var objb = new classb ("blue", "Nicolas ");
18
19 obja. saycolor (); // red
20 objb. saycolor (); // blue
21 objb. sayname (); // Nicolas
22

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.