JS about "Call method" Baidu, Arishu difficult to face questions

Source: Internet
Author: User

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

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.