Each function object in JavaScript has an apply () method and a call () method, each of which has the following syntax:
/* apply () method */ function . Apply (thisobj[, Argarray]) /* Call () method */ function. Call (thisobj[, arg1[, arg2[, [,... ArgN]]);
Their respective definitions:
Apply: Invokes one method of an object, replacing 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.
Example code:
(1) Basic usage
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);
(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);
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), which makes it easy to get the largest item in an array (apply will convert an array to a parameter
Number of ways passed to the method)
This block in the call when the first parameter gave 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:
var arr1=New Array ("1", "2", "3"); var arr2=New Array ("4", "5", "6"); Array.prototype.push.apply (ARR1,ARR2); // gets the length of the merged array, because push returns the length of an array
It can also be understood that arr1 invokes the push method, which is a collection of arguments that convert an array to a parameter list by using 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 apply () and call ()