JS inside the call, apply and bind

Source: Internet
Author: User

Reference this article: Http://www.tuicool.com/articles/EVF3Eb

Give a few examples

function Add (A, b)  {      alert (a+b);  }  function sub (b)  {      alert (a-b);  }  Add.call (Sub,3,1);

What is this run? Answer:

4 the. Run is Add. Because call is the caller of the run. Use the first parameter as the This parameter. 

One more example.

function Animal () { This. Name ="Animal";  This. ShowName =function () {Console.log ( This. Name); }} function Cat () { This. Name ="Cat"; }    varAnimal =NewAnimal (); varCat =NewCat (); Animal.showName.call (Cat,","); or animal.showName.apply (cat,[]); 

The answer is:

Cat. Because this changes the cat.

In addition, inheritance is used to invoke the constructor of the parent class :

function Animal (name) {          this. Name = name;            this. ShowName = function () {              Console.log (this. Name);          }      }      function Cat (name) {        Animal.call (this, name);    }       var New Cat ("Black cat");     cat.showname (); 

After calling the parent class, CAT also has a showname method.

The following two types of calls are basically equivalent:

Myfunc.call (func,"var","  fun"); Myfunc.apply (func,[  "var","  fun"]);

And about bind: function: Change the context of this

There are two different bind and call points:

The return value of ①bind is a function.

Using BIND is the function that returns the change context after this

Using call is to change the context of this and execute the function

There are also differences in the use of parameters behind ②

function f (a,b,c) {console.log (a,b,c);}varF_extend = F.bind (NULL,"extend_a") F ("A","B","C")//this will output--A B CF_extend ("A","B","C")//this will output--extend_a A BF_extend ("B","C")//this will output--extend_a B CF.call (NULL,"extend_a")//this will output--extend_a undefined undefinedcall is to pass the second and subsequent arguments as an argument to the F method, and bind, although the second and later parameters are used for the subsequent execution of the method, the arguments passed in the f_extend are then queued on the basis of the arguments passed in the bind. 

So, the following two are equivalent:

var f_extend = F.bind (null,"extend_a")//↓↓↓  var f_extend = function (b,c) {    return f.call (null," extend_a " , b,c);}

There is one application scenario:

For example, there is now a method to deal with different file types, and bind can create a simplified version of the processing method function Filedealfunc (type,url,callback) {if(type=="txt"){...} Else if(type=="XML"){...} .....}varTxtdealfunc = Filedealfunc.bind ( This,"txt");//It's more convenient to use.Filedealfunc ("txt", Xxurl,func);//turns outTxtdealfunc (xxurl,func);//now

For older versions, compatibility processing can be done in the following ways (the method called Bind is extended in ECMASCRIPT5 (ie6,7,8 not supported))

if (! Function.prototype.bind) {    = Function (obj)                    {varthis = arguments;         return function () {            1));     }}}

However, the above function seems to accept only one parameter.

JS inside the call, apply and 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.