The difference between apply, call, and bind in JavaScript

Source: Internet
Author: User

In JS, these three are used to change the function of the this object to the point, they have what kind of difference.
Before we tell the difference, we should summarize the similarities of the three:
1, are used to change the function of the this object point.
2. The first parameter is the object to which this is directed.
3. You can use the following parameters to pass the parameter.
So where are their differences, first look at an example.

        var melon = {            ' melon ',            ' female ',            ' 2 ',            function (school,grade) {                This]age + ' old ')            }        }          var little = {            ' little ',            ' Male ',            ' 1 '                }

Melon.say ();

In itself there is nothing to say, the show is definitely I am a girl, I am a year old.
So how to use the melon say method to display the little data.
For call you can do this:

Melon.say.call (little);

For apply you can do this:

Melon.say.apply (little);

And for bind, you need this:

Melon.say.bind (Little) ();

If the direct write Melon.say.bind (little) will not have any results, see the difference? Both call and apply are direct calls to the function, and the Bind method returns a function, so it needs () to be called later.
So what's the difference between call and apply? Let's rewrite the example a little bit.

 var  melon = {name:  ' Me Lon ' , Sex:  ' female ' , Age:  ' 2 ' , say:  function   (School,grade) {alert ( ' I am ' + , Sex:  ' man ' , Age:  ' 1 '  

We can see that the say method has more than two parameters, and we use the parameters of call/apply to pass the parameter.

For call, that's it.

Melon.say.call (Little, ' experimental primary school ', ' first grade ');

And for apply, that's it.

Melon.say.apply (little,[' first primary school ', ' second Grade ');

See the difference, the argument behind call is one by one corresponding to the Say method, and the second argument to apply is an array, and the elements in the array correspond to one by one of the Say method, which is the biggest difference between the two.
So how does bind pass? It can pass the argument like call.

Melon.say.bind (Little, ' experimental primary school ', ' first Grade ') ();

However, since BIND is still returning a function, we can also pass the arguments at the time of the call.

Melon.say.bind (Little) (' Huan Cheng Primary School ', ' third grade ');

The difference between apply, call, and bind in JavaScript

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.