Interview Title: The Call method in JavaScript

Source: Internet
Author: User
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.

Related Article

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.