In JavaScript these four goods usually appear together introduction, landlord memory is too bad often forget usage, so remember this article.
Apply and call
Apply and call are a method of a function prototype, and the caller's type must be a function. Official explanation: Apply one method of an object to replace the current object with another object. This is typically used to change the context, which is similar to bind. The difference between apply and call: The method passes different parameters. Apply passes an array, and if the wood has a second argument, the result is the same.
How to use: Fun.call (thisarg[, arg1[, arg2[, ...]) Fun.apply (Thisarg, [arg1,arg2,... ArgN])
Fun.call (this, arg1,arg2,arg3) = = Fun.apply (this, arguments) = = This.fun (arg1, arg2, Arg3)
function fn () { console.log (this.count);} var count = 10;var obj = {count:20};fn (); 10fn.call (); 10fn.call (window); 10fn.call (NULL); 10fn.call (obj); 20fn.apply (window); 10fn.apply (NULL); 10fn.apply (obj); 20
If this is not specified or NULL, the default point is to window.
The advantage of using apply is that you can directly pass in the arguments object of the current function as a second parameter, which plays a huge role in the object inheritance.
Calculates the maximum value of the array var a = [1, 2, 3, 6, 5, 4, 9, 8, 7];var MAXN = Math.max.apply (null, a); Console.log (MAXN); 10
Callee
Callee returns the function object being executed, which is the body of the specified function object. Arguments.length is the length of the argument, and the arguments.callee.length is the length of the parameter.
function fn (A, B, C, D) { console.log (arguments.length); 3 Console.log (arguments.callee.length);//4 Console.log (fn.length);//4 Console.log ( Arguments.callee = = = fn); True Console.log (A.callee); UNDEFINED}FN (1, 2, 3);
Perhaps because the landlord knows less, callee the use of the landlord only saw the form of Arguments.callee (perhaps there are more, so this article title for a preliminary understanding, looking through the Big guide ...) ), perhaps you will have doubts, the above code Arguments.callee can not be completely replaced with FN? In fact, if it is an anonymous function, Arguments.callee will be useful:
Prints the Fibonacci sequence within 100 (function (A, b) { var c = a + B; if (C >) return; Console.log (c); Arguments.callee (b, c);}) (-1, 1);
Caller
Caller returns a reference to a function (the return value is a function) that invokes the current function (the function that calls caller is the Arguments.callee in the code below). For a function, the caller property is defined only when the function executes. If the function is called by the top level of the Javascript program, then caller returns NULL.
function test () { console.log (Arguments.callee); Console.log (Arguments.callee.caller);} function Test2 () { test ();} Test (); Test2 ();//Test () {:} null//Test () {..} Test2 () {..}
The code above, the explanation seems to be logical. First execute the test function, Arguments.callee returns the function itself (test), which is the top-level call, returns NULL, the second call to the Test2 function, the Arguments.callee value is not the same as the test, and the test function is called in test2, so the Test2 function is returned.
function. Caller, returns the function.
Preliminary understanding of apply, call, callee, caller