Call: Changes the current execution context's this pointer
function dog (color) { this . Color = color;} Dog.prototype.eat = function () { return this . color+ "Dog can eat food" ;} var Blackdog = new Dog (' Black ' // Black dog can eat food var reddog = {color: " Red "}blackdog.eat.call (Reddog); // red dog can eat food
Summary: The first Blackdog object has this only to itself, so This.color is the black that is passed in when the object is instantiated, but when the BlackDog.eat.call (Reddog) method is executed, This is changed by the call method, this points to Reddog, so This.color is red.
Apply: The same as the call method, changing the current execution context's this pointer, but the passed in parameter (outside the first) is an array.
functionDog (color) { This. color =color;} Dog.prototype.showSkills=function(eat, run, jump) {Console.log ( This. Color + "dog skill:" + eat + "," + Run + "," +Jump );}varBlackdog =NewDog (' Black ');varReddog ={color:"Red"}blackdog.showskills.call (Reddog,"Eat", "Run", "Jump");//Red Dog Skill:eat,run,jumpBlackDog.showSkills.apply (Reddog, ["Eat", "Run", "Jump"]);//Red Dog Skill:eat,run,jump
Summary: It can be seen that call and apply usage are roughly the same, but the other method that is called by apply is that multiple arguments passed in are data, and multiple parameters in the array correspond to each receiver in their original order.
Bind: The function is similar to call and apply its role is to change the this pointer of a method, and it will take effect when the method is called, and not as calls and apply will understand the execution
EG1:functionBinddemo () {Console.log ( This. a)}; Binddemo.bind ({a:"I Am THIS.A"})//It's not being dropped, it's just a statement.Binddemo.bind ({A: "I am THIS.A"});//I am THIS.AEG2:functionCat () { This. Eat =function() {Console.log ( This. Name + "Cat is eating"); } }varCat =NewCat (); Cat.eat.call ({name:"Blackcat"});//Blackcat cat is eatingCat.eat.apply ({name: "RedCat"});//redCat cat is eatingCat.eat.bind ({name: "Othercat"});//only one object is returned here, there is no output, because there is no real callCat.eat.bind ({name: "Othercat"}) ();//othercat cat is eating
Summary: When the Bind method is used again, it must be dropped for execution, and call and apply will drop the original method and change the this pointer.
Learn some JS call apply bind new Harvest, do a record, I hope you have a lot of guidance