apply () method, and call () method

apply()&call()

  Before it comes to code once and apply on the call, there was no time to sort out today, taking advantage of idle time to sort out the differences and use of two methods.

  In fact, every function has a function apply () and call () method, the same as their role is in the specific function calls scope, equal to the set point of this function in vivo

  definition

  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

As can be seen from the definition, and apply both Call call a method of an object, replacing the current object with another object. The difference is that the transmission parameters, apply a maximum of only two parameters - and a new array of this object argArray, if the array will not error TypeError arg;

a plurality of call parameters can be passed, and apply the first parameter, it is used to replace the object is behind the argument list.

The basic syntax

/*apply()方法*/
function.apply(thisObj[, argArray])

/*call()方法*/
function.call(thisObj[, arg1[, arg2[, [,...argN]]]]);

  Usage examples

  apply()

 function Pet(words){
        this.words = words;
        this.speak = function () {
            console.log( this.words)
        }
    }
    function Dog(words){
        //Pet.call(this, words); //结果: Wang
       Pet.apply(this, arguments); //结果: Wang
    }
    var dog = new Dog('Wang');
    dog.speak();

  call()

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    </body>
    <script type="text/javascript">
        var obj = {
            name: "liSen",
            speak: function(say) {
                console.log(say + " " + this.name)
            }
        }
        obj.speak("hello");     // Results: Speak Lisen 
        
        var obj2 = { 
            name: " riwen " 
        } 
        
        // use this call change point 
        obj.speak.call (obj2, " Hello " );     // Results: Hello riwen 
    </ Script> 
< / html>

 

Different points: the different ways the received parameters.

  • apply () method  takes two parameters, a scope is (this) function to run, the other array of parameters.

Syntax:apply([thisObj [,argArray] ]); call a method of an object, another object 2 to replace the current object.

Note: If not a valid array argArray arguments object or not, this would result in a 
TypeError, and if no argArray thisObj any parameter, then the object is used as Global thisObj.

  • call () method of  the first parameter and apply the same () method, but must be passed to the function enumerated.

Syntax:call([thisObject[,arg1 [,arg2 [,...,argn]]]]); A method of application of an object, replacing the current object with another object.

Description:  Call method may be used instead of a call to another object method, the object may be a method Call function for the new object context change from the initial thisObj specified context, if no thisObj parameters, then the object is used for Global thisObj

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)

<Script type = " text / JavaScript " >
         // find the largest items in the array 
        var ARR = [ . 1 , . 3 , . 5 , . 7 , . 8 , . 9 , 12 is , 45 ];
         var max = Math.max.apply ( null , ARR); 
        the console.log (max); 
        // find the minimum array 
        var min = Math.min.apply ( null , ARR); 
        the console.log (min);
     </ Script>

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

 

Guess you like

Origin www.cnblogs.com/lxylhj/p/11517167.html