Midgard: A detailed description of apply, call, bind

Source: Internet
Author: User

Call Method:

Syntax: Call (Thisobj, ' ', '...) Definition: Invokes one method of an object, replacing the current object description with another object: The call method can be used to invoke a method in place of another object. The call method changes the context of the object context of a function to a new object specified by Thisobj, and if the Thisobj parameter is not provided, the global object is used as a thisobj

Apply method:

Syntax: Apply (thisobj,[",", ".....]) Definition: Applying the method of one object, replacing the current object with another object if Argarray is not a valid array or is not a arguments object, That would result in a typeerror. If no parameter is provided for Argarray and Thisobj, then the global object will be used as a thisobj and cannot be passed any arguments

In the same place:

1, are used to change the function of the This object point 2, the first parameter is this to point to the object 3, you can take advantage of the following parameters are different:1, call and apply are direct calls to the function, The Bind method still returns a function, so it needs to be followed by a () call to

Case one: The difference between bind and call, apply

varA={name:' Auntie Please don't introduce your daughter to me ', Sex:Male, Age:' 24 ', say:function() {Console.log ( This. name+ ', this year ' + This. age+ ' old, sex ' + This. Sex)}}varb={name:' I'm going to introduce you to the girl. ', Sex:Female, Age:' 18 '}a.say ();//Auntie Please don't introduce your daughter to me, 24 years old this year, sex maleSo let's think about how to get B to display the data in a say way. Using the Call method: A.say.call (b) with the Apply method: A.say.apply (b) with the Bind method: A.say.bind (b) ()

Case two: The difference between call and apply

varA={name:' Auntie Please don't introduce your daughter to me ', Sex:Male, Age:' 18 ', say:function(School,grade) {Console.log ( This. name+ ' sex ' + This. sex+ ' age ' + This. age+school+grade)}}varb={name:' I like to introduce you to the girl. ', Sex:Female, Age:' 18 'now we see two more parameters in the say method, and we use the call method with the call and apply parameters: A.say.call (b,' Tsinghua ', ' third grade ') Use the Apply method: A.say.apply (b,[' Tsinghua ', ' six Grade ']) The argument behind call corresponds to one by one in the Say method, and the second argument to apply is an array with the Bind method: A.say.bind (b,' Tsinghua ', ' third grade 'But since bind returns is still a function, we can do this at the time of the call A.say.bind (b) (' Tsinghua ', ' third grade ')
Since IE6-IE8 does not support this method, we want to use it in a different browser so we have to do the processing compatible with BIND mode
if (! Function.prototype.bind) {     Function.prototype.bind=Function(obj) {          var this=this;           var args=arguments;           return function () {               this.apply (Obj,array.prototype.slice.call (args,1)}      }}

Note: There are still some imperfect parts that will continue to be updated. Look Alive! Come on, you're all the best.  ?!! Another great God can point out the shortcomings

Midgard: A detailed description of apply, call, bind

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.