In the Learning JS process How also around the use of call, apply method, feel all the same, now look at their usage, the difference
In JavaScript, call and apply are meant to change the context in which a function is run, in other words, to change the direction of this within the function body.
e.g.
functionFruits () {}fruits.prototype={color:"Red", say:function() {Console.log ("My color is" + This. color); }}varApple =NewFruits;apple.say (); //My color is redBanana={color:"Yellow"}apple.say.call (banana); //My color is yellowApple.say.apply (banana);//My color is yellow
Banana itself has no say method, but in this way can say method, runtime context is not Apple, is also banana, so the display is the My color is yellow
The difference between apply and call
For both apply and call, the function is exactly the same, except that the parameters are accepted in a different way.
。 For example, there is a function defined as follows:
varfunc = function(arg1, arg2) { };
You can invoke the following method:
func.call(this, arg1, arg2);func.apply(this, [arg1, arg2])
Where this is the context you want to specify, he can be any JavaScript object (everything in JavaScript), call needs to pass parameters in order, and apply is to put the parameters in the array.
In JavaScript, the number of arguments for a function is not fixed, so if you want to apply the condition, call is used when your parameters are clearly known.
Use apply when unsure, and then pass in the parameter push into the array. When the number of arguments is indeterminate, the function can also traverse all the arguments by arguments the array.
A simple example of bind usage
//use variables to save this value under normal conditionsvarFoo ={bar:1, Eventbind:function(){ var_this = This ; $('. SomeClass '). On (' click ',function(event) {/*Act on the event*/Console.log (_this.bar); //1 }); }}//using bind for function bindingvarFoo ={bar:1, Eventbind:function(){ $('. SomeClass '). On (' click ',function(event) {/*Act on the event*/Console.log ( This. bar);//1}.bind ( This)); }}
In the above code, bind () creates a function that, when the Click event is bound to be invoked, its this keyword is set to the value passed in (this refers to the parameter passed in when bind () is called). So here we pass in the desired context this (actually foo), into the bind () function. Then, when the callback function is executed, this point points to the Foo object
Apply, call, bind comparison
var obj = { bayi,}; var foo = { function() { returnthis. x; }} Console.log (Foo.getX.bind (obj) ()); // Bayiconsole.log (Foo.getX.call (obj)); // Bayiconsole.log (foo.getX.apply (obj)); // Bayi
All three outputs are 81, but note that with the bind () method, he has more pairs of parentheses behind him.
That is, the difference is that the bind () method is used when you want to change the context and not execute immediately, but rather when the callback executes. The Apply/call executes the function immediately.
Let's summarize:
- Apply, call, and bind are all pointers to the This object that is used to change the function;
- Apply, call, bind the first parameter is the object that this is to point to, that is, the context you want to specify;
- Apply, call, bind three can use the following parameters to pass the parameter;
- Bind is to return the corresponding function, which is convenient to call later;apply,call is called immediately.
Reference: http://www.admin10000.com/document/6711.html
JS's call, apply