Topic:
function fn (A, b) { console.log (this); Console.log (a); Console.log (a+b);} Fn.call (1); Fn.call.call (FN); Fn.call.call.call (fn,1,2); Fn.call.call.call.call (fn,1,2,3);
Answer:
Fn.call (1);//1,undefined,nan
Fn.call.call (FN);//Fn,undefined,nan
Fn.call.call.call (fn,1,2);// 1,2,nan
Fn.call.call.call.call (fn,1,2,3);//1,2,5
Deep problem Solving ideas:
Fn.call (1); The first parameter of call changes the keyword in the function in the call before the this so the output 1, there is no parameter, so a, B is undefined, the sum result is Nan;
Fn.call.call (FN); This piece is a difficult point, but it is also very well understood! Fn.call find the Call method on the Function.prototype (this is also a function, is also an instance of the function class
, you can continue to invoke call/apply, etc.) we can think of fn.call as a function a Then it is equal to A.call (FN), this
executes the call method, the keyword in a is modified to the function FN, and then the function A (fn.call) execution;
Fn.call.call.call (fn,1,2); We can think of Fn.call.call.call as a (Fn.call.call) by the way of the previous prototype chain. Call execution, where the parameter FN in
is already executed as a function, so it is executed by A.call! 1 changes the This in the function of the call before the first argument, and the argument after the
is passed as an argument to the function's formal argument!
Fn.call.call.call.call (fn,1,2,3); the same principle!
Summary:
Do not understand the words can also remember this general trick:
encountered two and two or more call is to let the first parameter execution, the first parameter must be a function;
The second parameter is to change the first parameter in this;
The third and third arguments are passed as arguments to the first argument.