apply and call

Red font need to explain the video analysis

 

call:

function.call(obj[,arg1[, arg2[, [,.argN]]]]])

  

  • Call the callobject must be a function function
  • callThe first parameter is the function will change the context of the object pointed to, that is, in the above example Xiaogang, the above example is His wife, if not pass, it will default to the global objectwindow
  • The second parameter starts may receive any number of parameters that will be passed as a function parameter for function
  • Call the callmethod will be executed immediately

apply:

function.apply(obj[,argArray])

And callmethods of use are basically the same, but receives only two parameters, wherein the second parameter must be an array or an array type, which is both a very important distinction method

 



Same point

Methods are able to change the execution context (execution environment), a method of one object to another object is performed, and is performed immediately

 

difference

callThe method begins parameters may be received from any of the second parameters, each parameter is mapped to a corresponding position func parameter , the parameter can be called by name, but if all the parameters passed as an array, as a whole they map func parameter to the corresponding first, then the parameters are null



FUNC function (A, B, C)} { 

func.call (obj, l, 2,3) 
// function actually received parameter l, 2,3 

func.call (obj, [l, 2,3 ]) 
// function arguments actually received [1,2,3], undefined, undefined

  

applyThe method of at most two parameters, the second parameter receiving arrays or array type , but will be converted to an array of incoming func class, and will be mapped to corresponding parameters func

 

func.apply (obj, [l, 2,3]) 
// function actually received parameter l, 2,3 

func.apply (obj, { 
    0:. 1, 
    . 1: 2, 
    2:. 3, 
    length: . 3 
}) 
// function arguments actually received 1,2,3

  

Two methods how to choose?

With simple, according to your argument passed to make the selection, you do not need to pass only when a parameter or parameters, use callwhen you want to pass multiple objects, useapply

Or, if you need to pass the argument it is already a class or an array of arrays, use apply, if you still need a separate by-passed, consider using call(if you took the trouble of words)


[Other uses - object inheritance]

Because you can change the thispoints, so it can achieve the object of inheritance

function superClass () {
    this.a = 1;
    this.print = function () {
        console.log(this.a);
    }
}

function subClass () {
    superClass.call(this);
    this.print();
}

subClass();
// 1

  

subClassBy callway inherits superClassthe printmethods and avariables, but subClassalso can expand their other methods

 
 

Array class with an array of small science

Array We all know what it is, its features are what it?

  1. By calling the subscripts, such as array[0]
  2. Having a length attributelength
  3. And for loop can forEachbe traversed methods

Array of class definition, it should have substantially the same feature array, to know, as a form below the object is a class object array

var arrayLike = {
    0: 'item1',
    1: 'item2',
    2: 'item3',
    length: 3
}

  

Array class arrayLikecan be called by the subscript having a lengthproperty, but also can be traversed by a for loop

Node obtain dom way we often use is the return of a class array, use in a method in argumentswhich a keyword to get all of the parameters is also an array of class

But the array of classes but can not forEachbe traversed as forEacha method on an array prototype chain, after all, not an array of array data, can not be used




Guess you like

Origin www.cnblogs.com/chargeworld/p/12233631.html
Recommended