The difference between call and apply

Source: Internet
Author: User

the difference between call and apply:

Their respective definitions:

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.

  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.

One: Sample code: (Basic usage)

1)

Usage of apply:

 function   add (b) { return  a+B;  function   Sub (A, b) { return  a-B;  }  var  a1 = add.apply (sub,[4,2]); // sub Call the Add method   var  a2 = sub.apply (Add,[4,2 // 6  alert (A2); // 2  /*  call usage  */ var  a1 = Add.call (sub,4,2);//Multiple Parameters 

2) Implementing Inheritance

functionAnimal (name) { This. Name =name;  This. ShowName =function() {alert ( This. Name); }    }functionCat (name) {animal.apply ( This, [name]); }varCat =NewCat ("Cuckoo"); Cat.showname ();/*use of call*/Animal.call ( This, name);

3) 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

Some other clever uses of apply

  (1) Math.max can be implemented to get the largest of the array:

Because Math.max does not support Math.max ([PARAM1,PARAM2]), which is an array, it supports Math.max (param1,param2 ...), so you can solve Var max= according to the characteristics of apply. Math.max.apply (Null,array), so that it is easy to get the largest item in an array (apply will convert an array to a parameter to a parameter to pass to the method) this block is called when the first argument to NULL,    This is because there is no object to call this method, I just need to use this method to help me to calculate the return of the results on the line, so directly passed a null past. This method can also be implemented to get the smallest item in the array: Math.min.apply (Null,array) (2) Array.prototype.push can implement merging of two arrays the same push method does not provide an array of push,          But it provides push (Param1,param2...paramn), and can also use apply to convert the array, namely: 1.var arr1=new Array ("1", "2", "3");          2.var arr2=new Array ("4", "5", "6");    3.array.prototype.push.apply (ARR1,ARR2); The length of the merged array is obtained, because push is the length of the returned array, which is also understood, ARR1 calls the push method, and the parameter is converted to the collection of argument lists by apply Under what circumstances, you can use a special usage like Math.max and so on:           In general, the target function only needs n parameter list, and does not receive an array form, you can solve this problem skillfully by the way of apply.

The difference between call and apply

Related Article

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.