arguments, caller, callee, this is a special set of objects that are used in function. Apply () and call () are different ways of calling a function.
Arguments
Can be used to get the actual array of function-passing variables. This variable is particularly suitable for writing "polymorphism" functions, that is, you can do different things depending on the number of incoming arguments.
Example One – Add function
function sum () { var total = 0; for (var i=0; i< Arguments.length; I++ += Arguments[i]; return Total;} // test log (sum ()); // log (sum (1, 2)); // log (sum (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); //
Example II – Self-introduction function
functionintroduce () {varcallback =NULL; varmsg = "; for(vari=0; i<arguments.length; i++ ) { varp =Arguments[i]; if(typeofp = = ' string ') {msg+ = ' My name ' +p; } Else if(typeofp = = ' number ') {msg+ = ' This year ' +p+ '; } Else if(typeofp = = ' function ') {Callback=p; } } if(Callback! =NULL) {callback (msg); } Else{log (msg); }}//Tests 2Introduce (' David ');//Console prints out "My name is David."Introduce (' David ', 29);//Console Prints "I'm David 29 years old."Introduce (' David ',function(msg) {//jump out of "my name David."alert (msg);}); Introduce (' David ', 29,function(msg) {//"I'm David 29 years old."alert (msg);});
- Callee
This is one of the arguments's nature, which can be obtained by the call function itself.
- Caller
Can be used to get the source object of the call function.
- This
Refers to the owner of the function.
- Apply () and call ()
Apply and call both have the same function, and can be used to specify the this change in the call function.
The differences are different in the way they are written:
Apply (Thisarg, argarray ); The second one must be an array, or it will produce the wrong error of the parameters
Call (Thisarg[, Arg1, arg2 ...] );
The following example shows the use of callee, caller, this and apply (), call ()
functionMethodA (P1, p2, p3) {log (' ======================== '); Log (arguments); //actually entering the arrayLog (Arguments.callee);//point to MethodALog (Arguments.callee.caller);//refers to the object of call MethodALog (' declaring parameters ' length: ' +arguments.callee.length); Log (' Actual length: ' +arguments.length); Log ( This ); Log (p1); Log (p2); Log (p3);}functionHandlecaller () {MethodA (' xxx ', ' yyy '); Methoda.apply (Handlecaller, [' xxx ', ' yyy ']);//Specify this object}functioninit () {Handlecaller (); Methoda.call (Handlecaller,' xxx ', ' yyy ');//Specify this object}init ();
Results:
Notice that the first result block of this refers to the Window object, while the other two results refer to the Handlecaller
The third result block's function caller refers to Init, and the other two perform results refer to Handlecaller
The log function– used in the example above is used to output text to Firebug or Chrome, IE8 console
function log (msg) { if(window.console) { console.log (msg);} }
References:
Http://www.ijavascript.cn/jiaocheng/caller-callee-call-apply-464.html
Http://blog.darkthread.net/blogs/darkthreadtw/archive/2009/03/11/js-this-and-closure.aspx
Http://blog.darkthread.net/blogs/darkthreadtw/archive/2009/04/10/js-func-apply.aspx
Turn from (http://fstoke.me/blog/?p=1932)
Internal objects and functions in javascript: arguments, callee, caller, this, apply (), call ()