This is typically used to specify the environment for this, which is usually the problem before it is learned.
var a = { User:"xmz", fn:function() { Console.log ( this. user);} } var b =//undefined
We want to print the user in object A but print out what's going on with the undefined? If we directly execute A.fn () is possible.
var a = { User:"xmz", fn:function() { Console.log ( this. user); // Xmz
This can be printed because the this here is pointing to function A.
Although this method can achieve our goal, but sometimes we have to save this object to another variable, then you can use the following methods.
1. Call ()
var a = { User:"xmz", fn:function() { Console.log ( this//xmz }}var b = A.fn;b.call (a);
By the call method, the first parameter is added to which environment to add B to, in a nutshell, this will point to that object.
The call method can add multiple parameters in addition to the first argument, as follows:
var a = { User:"xmz", fn:function(e,ee) { Console.log ( This // XMZ // 3 }}var b = A.fn;b.call (A,N);
2. Apply ()
The Apply method is somewhat similar to the call method, and it can also change the point of this
var a = { User:"xmz", fn:function() { Console.log ( this//xmz }}var b = a.fn;b.apply (a);
The same applies can have multiple parameters, but the difference is that the second parameter must be an array, as follows:
var a = { User:"xmz", fn:function(e,ee) { Console.log ( This // XMZ // One }}var b = a.fn;b.apply (a,[10,1]);
Note If the first argument to call and apply is NULL, then this refers to the Window object
var a = { User:"xmz", fn:function() { Console.log ( this//Window {external:object, Chrome:object, Document:document, A:object, speechsynthesis: Speechsynthesis ...} }}var b = a.fn;b.apply (null);
3. Bind ()
The Bind method is somewhat different from the call and apply methods, but they can be used to change the point of this anyway.
Let's talk about their differences first.
var a = { User:"xmz", fn:function() { Console.log ( this. user);} } var b = A.fn;b.bind (a);
We found that the code was not printed, yes, that's the difference between bind and call, the Apply method, and the Bind method actually returns a modified function.
var a = { User:"xmz", fn:function() { Console.log ( this. user);} } var b = A.fn; var c =//function () {[native code]}
So let's run the function C and see if we can print out the user in object A.
var a = { User:"xmz", fn:function() { Console.log ( This//xmz }}var b = A.fn; var c = b.bind (a); C ();
The same bind can have multiple parameters and can be added again when the parameters are executed, but it is important to note that the parameters are in the order of the formal parameter.
var a = { User:"xmz", fn:function(e,d,f) { Console.log ( This // XMZ // Ten 1 2 }}var b = A.fn; var c = b.bind (a,10); C (N);
Summary: Call and apply are changes in the context of this and immediately execute this function, the bind method can let the corresponding function when it is called when the calling, and the parameters can be added at the time of execution, this is their difference, according to their actual situation to choose to use.
JS in the use of call, apply, bind