apply () and call ()

JavaScript Function Each object has an apply () method and a call () method, their syntax are:

/ * Apply () method * /
function.apply(thisObj[, argArray])

/ * Call () method * /
function.call(thisObj[, arg1[, arg2[, [,...argN]]]]);

Their definitions:

apply: a call to an object method, replacing the current object with another object. For example: B.apply (A, arguments); i.e., A B object application object method.

call: calling a method of an object, replacing the current object with another object. For example: B.call (A, args1, args2); i.e. the object A calls object B method.

They have in common:

All "can be used instead of a call to another object method, the object is a new object function thisObj context specified by the context change from the initial."

Their differences:

apply: only up to two parameters - this new objects and an array argArray. If you pass multiple parameters to the method, put the parameters are written into the array which, of course, even if only one parameter, also written into the array. If argArray not a valid array of objects or arguments, it would result in a TypeError. If no parameter argArray any thisObj, then the object is used as Global thisObj, and are not pass any parameters.

call: it can accept a plurality of parameters, the first parameter and apply, as it is behind a string argument list. This method is mainly used when the subject of the methods js call each other, so that the current pointer is consistent with this example, this pointer needs to be changed or in exceptional circumstances. If no thisObj parameters, then the Global object is used as thisObj. 

In fact, apply and call the function is the same, only different parameters passed in the form of a list.

Sample code:

(1) Basic usage

Copy the code
function add(a,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]);
alert(a1);  //6     
Alert (A2); // 2 

/ * Call usage * /
var A1 = add.call (Sub, 4,2);
Copy the code

(2) implementation inheritance

Copy the code
function Animal(name){
  this.name = name;
  this.showName = function(){
        alert(this.name);    
    }    
}

function Cat(name){
  Animal.apply(this,[name]);    
}

var cat = new Cat ( "cooing");
cat.showName ();

/ * Call usage * /
Animal.call(this,name);
Copy the code

 (3) multiple inheritance

Copy the code
function Class10(){
  this.showSub = function(a,b){
        alert(a - b);
    }   
}

function Class11(){
  this.showAdd = function(a,b){
        alert(a + b);
    }  
}

function Class12(){
  Class10.apply(this);
  Class11.apply(this);   
  // Class10.call(this);
  //Class11.call(this);  
}

var c2 = new Class12();
c2.showSub(3,1);    //2
c2.showAdd (3,1); // 4
Copy the code

 

apply some other clever uses

(1) Math.max can get the array to achieve a maximum of:

Because Math.max does not support Math.max ([param1, param2]) is an array, but it supports Math.max (param1, param2 ...), it is possible to solve var max = Math.max according to the characteristics apply. apply (null, array), so that you can easily get a maximum entry in the array (an array Apply will be converted to a parameter by one parameter

Method to transfer a number of ways)

When this first parameter in the call to the null, because no object to call this method, I only use this method to help my operation, and the results returned on the line, so a direct pass a null in the past.

This method can also be achieved with the minimum entry in the array obtained: Math.min.apply (null, array)

(2) Array.prototype.push merge the two arrays can be achieved

The same push method does not provide an array of push, but it provides a push (param1, param2 ... paramN), also can apply to change if this array, namely:

var arr1=new Array("1","2","3");
var arr2=new Array("4","5","6");
Array.prototype.push.apply (arr1, arr2); // get the combined length of the array, as is the return push a length of the array

Can be understood, arr1 calls the push method, parameters apply by converting the array is a set of parameter list

Usually under what circumstances, you can use special usage similar Math.max the like apply:

In general the objective function requires only a list of n arguments, without receiving an array of forms, can solve this problem by way of subtly apply.

// find the maximum value of the array 
     the let ARR = [1,2,3,4,5,6,7,8,9,10 ];
     let maxVal = Math.max.apply(null,arr);
     Alert ( "maximum arr array is:" + maxVal);

     // minimization array 
     the let minVal = Math.min.apply ( null , ARR);
     Alert ( "minimum arr array is:" + minVal);

     // merge the two arrays and returns the new length of the array 
    the let of arr1 = new new the Array ( ". 1", "2", ". 3", ". 4", ". 5", ". 6", ". 7", ". 8" ) ;
    let arr2 = new Array("11","22","33","44","55","66","77","88");
    let merge = arr1.concat(arr2);
    let mergeLen = Array.prototype.push.apply(arr1,arr2);

    console.debug(mergeLen);//16
    console.log(merge);//["1", "2", "3", "4", "5", "6", "7", "8", "11", "22", "33", "44", "55", "66", "77", "88"]

 

Guess you like

Origin www.cnblogs.com/kingchan/p/11099093.html
Recommended