The difference between apply and call

Source: Internet
Author: User

1. First find the definition of call () and apply ()

1 /* apply () method */ 2 function . Apply (thisobj[, Argarray]) 3 4 /* Call () method */ 5 function. Call (thisobj[, arg1[, arg2[, [,... ArgN]]);

For example: Function.apply (this,[1,2,3]); The first argument is the context, and the second argument is an array of arguments.

Function.call (this,1,2,3); The first argument is the context, followed by the argument sequence that the instance passed in.

Apply: One method of applying an object that replaces the current object with another object. For example: b.apply (A, arguments); A method that applies a B object to a object.

Call: Invokes one method of an object, replacing the current object with another object. For example: B.call (A, args1,args2); A method that a object calls a B object.

2. Example:

1 functionAdd (A, b) {2   returnA +b; 3 }4 functionSub (A, b) {5   returnA-b; 6 }7 varA1 = Add.apply (sub,[4,2]);//the sub calls the Add Method8 varA2 = sub.apply (add,[4,2]);9alert (A1);//6Tenalert (A2);//2 One  A /*use of call*/ - varA3 = Add.call (sub,4,2);//the sub calls the Add Method -Alert (A3);//6

3. Differences

What they have in common: All "can be used instead of another object to invoke a method that changes the object context of a function from the initial context to the new object specified by Thisobj".

The difference between them:

Apply: There can be at most two parameters-the new this object and an array Argarray. If you pass multiple arguments to the method, the arguments are written into the array, and of course, even if there is only one argument, it is written into the array. If Argarray is not a valid array or arguments object, it will result in a typeerror. If none of the Argarray and Thisobj parameters are provided, then the global object is used as a thisobj and cannot be passed any parameters.

Call: It can accept multiple parameters, the first parameter is the same as apply, followed by a list of parameters. This method is mainly used when the JS object method calls each other, so that the current this instance pointer is consistent, or in special cases need to change the this pointer. If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

In fact, the function of apply and call is the same, except that the parameter list is passed in as a different form.

4. Inheritance

1 functionAnimal (name) {2    This. Name =name;3    This. ShowName =function(){4Alert This. Name); 5     }    6 }7 8 functionCat (name) {9Animal.apply ( This, [name]); Ten } One  A varCat =NewCat ("Cuckoo"); - cat.showname (); -  the /*use of call*/ -Animal.call ( This, name);//cat method of calling Animal

5. Multiple inheritance

functionClass10 () { This. Showsub =function(A, b) {alert (a-b); }   }functionClass11 () { This. Showadd =function(A, b) {alert (a+b); }  }functionClass12 () {class10.apply ( This); Class11.apply ( This); //Class10.call (this);  //Class11.call (this); }varC2 =NewClass12 (); C2.showsub (3,1);//2C2.showadd (3,1);//4

The difference between apply and call

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.