Interview questions:
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, and no parameters, so a, B is undefined, the sum result is Nan;
Fn.call.call (FN); This piece is a difficult point, but also very good understanding! Fn.call find the Call method on Function.prototype (this is also a function, also a function class
, or you can continue to call call/apply and so on) we can think of fn.call as a function a then it is equal to A.call (FN), which
Execute the call method, change the keyword in a to the function FN, and then execute the function A (fn.call);
Fn.call.call.call (fn,1,2); by means of the previous prototype chain we can think of Fn.call.call.call as A (fn.call.call). Call execution, in this case
The parameter FN has been executed as a function, so it's a.call! 1 Change the This, as the first parameter in the function of call front The arguments in the are passed as arguments to the function's formal parameters!
fn.call.call.call.call (fn,1,2,3); the same principle!
summary:
The second parameter is to change the first parameter in This;
The third and third arguments are passed as arguments to the first argument.
JS about "Call method" Baidu, Arishu difficult to face questions