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.