Summary of Apply,call,bind differences in JavaScript, bind compatibility, etc.

Source: Internet
Author: User

1 Similarities between the three:

(1). are used to change the direction of the This object of the function

(2). Use the first parameter to point to the This object

(3). All parameters can be passed in.

So, what's the difference between them? Take a look at the following example:

Two objects, an animal, a human, human beings can also walk, then we can let humans directly use animal action method? The answer is of course possible.

The method is to change the direction of the animal's action method so that its direction becomes person, as follows:

The results are as follows:

Let's look at two other uses:

This is the call method, and the last bind is a bit special:

This is because bind gets to this method, but has already pointed the this point to the person, if you want to run, still need to call

The above is the use of these three, there is an important difference is that when the method needs to pass parameters, see the following example:

When we need to have parameters, apply uses the following:

The use of call is as follows:

That is, apply needs to be passed in the array of parameters, it will automatically map to the parameters of the method, and call is directly in order to write parameters, and finally look at the bind

Bind can pass parameters directly (as with call), but because it itself acquires a function, it can also be used to transmit parameters in the same way as the function arguments:

To this, the difference between these three methods is basically finished, hope can be helpful to interested people!

Summary of Apply,call,bind differences in JavaScript, bind compatibility, etc.

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: 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.