In JavaScript, call and the apply three methods that the object comes bind Function with, the main function of these three methods is to change the direction of the function this .
call, apply and bind the similarities and differences of the methods:, and the apply call three are bind used to change the function of this object point;
apply, call the bind first parameter of the three is the object to which this is to be pointed, that is, the context that you want to specify (each invocation of the function has a special value-the context of the call-this is the this value of the keyword.) );
apply, call and bind all three can use the following parameters to pass the parameter;
bindis to return the corresponding function, which is convenient to call later; apply call
Apply and call function exactly the same, just accept the parameters of different ways, call is one by one, apply is an array form (if not a valid array or not a arguments object, it will result in a TypeError)
A.call (Thisobj,arg1,arg2 ...)
A represents a method
thisObjThe following 4 conditions are available:
(1) Do not pass, or pass null,undefined, function of this point to the Window object
(2) A function name that passes another function, the this in the function points to the reference to the function
(3) to pass an underlying type, such as a string, numeric, or Boolean type, which in the function points to its corresponding wrapper object, such as String, number, Boolean
(4) Passing an object, which in the function points to this object
< Span class= "Hljs-params" >< Span class= "hljs-function" > A.call (); Span class= "hljs-comment" >//window
< Span class= "Hljs-params" >< Span class= "hljs-function" > A.call (null); //window
< Span class= "Hljs-params" >< Span class= "hljs-function" > A.call ( undefined); //window
< Span class= "Hljs-params" >< Span class= "hljs-function" > A.call (1); //numbera.call (//string
< Span class= "Hljs-params" >< Span class= "hljs-function" > A.call (true); //boolean
< Span class= "Hljs-params" >< Span class= "hljs-function" > A.call (b); //function B () {}
A.call (c); //object
bind is ES5 new method
The Bind method creates a new function called a binding function, and when called, the binding function takes the bind() first parameter of the method passed in when it is created this , passing bind() in The second and subsequent parameters of the method plus the arguments of the binding function itself are invoked in order as parameters of the original function to invoke the original function.
Note: bind The return value of the method is a function
Invocation mode
A.bing (Thisobj,arg1,arg2 ...) () equivalent to var fun = A,bind (thisobj,arg1,arg2 ...); Fun ();
Call Apply Bind