Red font need to explain the video analysis
call:
function.call(obj[,arg1[, arg2[, [,.argN]]]]])
- Call the
call
object must be a function function call
The 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
call
method will be executed immediately
apply:
function.apply(obj[,argArray])
And call
methods 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
call
The 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
apply
The 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 call
when 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 this
points, 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
subClass
By call
way inherits superClass
the print
methods and a
variables, but subClass
also 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?
- By calling the subscripts, such as
array[0]
- Having a length attribute
length
- And for loop can
forEach
be 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 arrayLike
can be called by the subscript having a length
property, 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 arguments
which a keyword to get all of the parameters is also an array of class
But the array of classes but can not forEach
be traversed as forEach
a method on an array prototype chain, after all, not an array of array data, can not be used