The thing about call and apply?

Source: Internet
Author: User

Changing the This keyword in the closure in JavaScript is often used for call and apply.

First:What is the difference between the role of call and apply?

A: The role of call and apply is the same. are used to change the function's this value and to pass the corresponding arguments to the function. The difference is that their value is different, call is a parameter to pass the value, that is, the value is hashed, and apply is to put all the parameters in an array, just pass this array is enough.

Application: In JS we often use some of the methods of the array. But the form of an array of classes cannot be directly used in the array method. Then we can use the call and apply methods to indirectly work with array arrays.

Enter code here
var ary=[{Name:"Gudaochuan", age:22},{Name:"Super", age:48},{Name:"Man", age:38}];
Console. log([]. Sort. Call(ary,function(a, b){return A. Age-b; ));

The following is the result of the above array output:

Here is an interview question: it is said that before the Jinshan, and now should not have this problem:

function fn1 () {alert (1)};

function fn2 () {alert (2)};

Fn3=fn2.call;
Fn2.call (FN1);
what is the result of the output and why

Fn3.call (FN1);//What is the result of the output, why

Fn2.call (FN1); This has nothing to say. FN2 does not have this in it, so call here (FN1) has little meaning. is equivalent to calling the Fn2 () method directly, which pops up 2

It's mainly the following.

Fn3=fn2.call;

Fn3.call (FN1);

What does Fn3=fn2.call mean?

*1. *call is the method defined on the prototype. So Fn2.call is just the definition of the call method ... It is no different from the definition of a common method. So Fn2.call==fn3==function.prototype.call

Function.protoType.call is the call method on the method prototype

So Fn3=fn1.call=fn2.call

*2. *fn2.call (NULL); Who's this fn2 here, and who's this in call?

The This in fn2 is cast to null. This is fn2 in call.

*3. *call is a function-type call is a method of prototyping, so call is a function type

So call can invoke the calling method

Fn3.call (FN1) ==function.call.call (FN1);

The last place to note is this: The method on the class this is the equivalent of who is calling this method. So

Function.call.call (FN1);

Function.call It's inside this point fn1 so it's equivalent to Fn1.call

So Fn3.call (FN1); pop up 1

The thing about call and apply?

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.