Learn some JS call apply bind new Harvest, do a record, I hope you have a lot of guidance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.