Javascript arguments, caller, and callee

Source: Internet
Author: User

Reposted keakon original http://www.keakon.net/2009/04/30/JavaScript%E7%9A%84arguments%E3%80%81caller%E5%92%8Ccallee

Well written and added to favorites.

The first is arguments.

It is a hidden object named arguments automatically generated within the function during function calling. This object is similar to an array. You can use the [] operator to obtain the real parameters passed when a function is called:

Function write (STR) {document. Write (STR = undefined? '': Str) + '<br/>');} function testarg () {write ('number of arguments: '+ arguments. length); For (VAR I = 0; I <arguments. length; ++ I) {write (arguments [I]);} write ();} testarg (123); testarg ('Hi'); testarg ('keakon ', 'lolil ');

Result:

Number of real parameters: 1
123

Number of real parameters: 1
Hi

Number of real parameters: 2
Keakon
Loli

Obviously, we can use this object to implement variable parameters and default parameters.

You can also specify the function to which arguments belongs:

Function write (STR) {document. Write (STR = undefined? '': Str) + '<br/>');} function acallee () {write ('actual number of arguments of acallee: '+ arguments. length); write ('aller's real parameter count: '+ acaller. arguments. length);} function acaller () {acallee (789);} acaller (123,456 );

Result:

Number of real parameters of acallee: 1
Number of acaller arguments: 2

That is to say, if the name of the function is not followed before arguments, the arguments of the current function is used; otherwise, the specified function is used.

In addition, although arguments acts like an array, technically, it is not an array object:

(function () {alert(arguments instanceof Array); // falsealert(typeof(arguments)); // object})();

In addition, the arguments object is created only when the function is called. Its value is null when the function is not called:

Alert (function () {}). Arguments); // or alert (new function (). Arguments );

Next let's look at caller.
When a function calls another function, the called function automatically generates a caller attribute pointing to the function object that calls it. If the function is not currently called, or is not called by other functions, caller is null.

function write(anObject) {document.write('<pre>' + (anObject == null ? 'null' : anObject.toString()) + '</pre>');}function testCaller() {var caller = testCaller.caller;write(caller);}function aCaller() {testCaller();}testCaller();aCaller();

Result:

Null
Function acaller (){
Testcaller ();
}

In fact, the function object can be directly written. I call tostring () to indicate that the function itself is not a string. However, due to the special nature of JavaScript, we can directly output function code without any disassembly process.

Finally, let's look at callee.
When a function is called, its arguments. callee object points to itself, that is, a reference to itself.
Because arguments is valid only when the function is called, arguments. callee does not exist (that is, null. callee) when the function is not called, and an exception occurs when it is called.
In addition, arguments. callee. length can be used to obtain the number of parameters. However, unlike arguments, arguments. callee cannot obtain the value of the form parameter (you should obtain the value of the real parameter ).
Because arguments can specify the function name, arguments. callee can also:

Function write (STR) {document. Write (STR = undefined? '': Str) + '<br/>');} function acallee (ARG) {write ('acallee's parameter count: '+ arguments. callee. length); write ('acaller parameter count: '+ acaller. arguments. callee. length); // equivalent to arguments. callee. caller. arguments. callee. length or arguments. callee. caller. length} function acaller (arg1, arg2) {acallee ();} acaller ();

Result:

Number of acallee parameters: 1
Number of acaller parameters: 2

Someone may ask callee about the usage of this object.

Because JavaScript Functions are actually objects, using this inside an object theoretically points to the object itself. But this is not the case in Javascript. in different contexts, it points to different objects.
Here is an article titled [illustration] JavaScript-"This" that you don't know, which describes this object well.

In short, a function has a scope (that is, the object in which it is created). Generally, this scope or object is the this object. (The apply and call methods can be used to change the scope. In addition, when an object is created using the new operator, this points to the object itself .)
When we define a function in the global scope (window object) and call it, its this object is the window object.
If you define an F method for the object OBJ and call it using obj. F (), the this object in the F method is obj.
That is to say, to get the reference of the F method in the F method, you need to use obj. f or this. f. But this is not a general method. If it is a G method, you have to change it to OBJ. g or this. G. However, if it is an anonymous function, it cannot be obtained in this way.
Therefore, the appearance of callee removes the coupling between methods and method names, making the method itself more universal. It also provides better readability for recursive calls and makes it possible to reference anonymous function objects:

Function Fibonacci (Num) {If (Num <3) {return 1;} else {return arguments. callee (Num-1) + arguments. callee (Num-2); // after changing the function name, an error will occur when calling Fibonacci} var F = Fibonacci; Fibonacci = NULL; alert (f (10); alert (function (Num) {If (Num <3) {return 1;} else {return arguments. callee (Num-1) + arguments. callee (Num-2) ;}}) (10 ));

As you can see, because arguments. callee is used, we can better understand that this is a recursive call of the call itself.
You can even pass this function object to other functions. Even if the function name of the function object no longer references the original function, you can still use the new function name to call the function.

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.