The function in the arguments
conversion method into a real array.
arguments
Is an array of classes, in addition to a similar array of arguments consisting of outside, as well as its own properties, such as callee
, arguments.callee
is a reference to the currently executing this function, it only exists only when the function is executed. Because when the function started, will automatically create the first variable arguments
object.
A, arguments
it is an array of type
-
It will save the argument as an array with, you can also be accessed like an array arguments, such as:
arguments[0]
; -
It also has its own unique attributes, such as:
callee
; -
Its length is the number of arguments.
Then
arguments.callee.length
what is it?arguments.callee
It is a reference to the function currently being performed, similar tofunction.length
that parameter number.
Second, the arguments
conversion method for real array
1. Array.prototype.slice.apply(arguments)
This is the operating efficiency faster way (see information that other people's), why can not an array, because the arguments
object has length
properties, and this method will be based on length
attributes, have a return length
array length. If the length
property is not number
, it returns the length of the array 0
;
So long as the other objects length
properties are also possible yo, as the object has attributes 0
, it is the corresponding arr[0]
, i.e., property is a natural number number
that corresponds to the array index, if the value is greater than the length, of course, to let go it.
2. Array.prototype.concat.apply(thisArg,arguments)
thisArg
The new array is empty, apply
the method will function this
point thisArg
, arguments
as a reference to the array-transmission apply
.
According to apply
the role of the method, it is about Array.prototype.slice
the method specified this
as thisArg
call inside and pass it. Note that using this method: if an array within the array, will be assembled into an array. The reason is that apply
parameter passing features.
3. I thought of a method, using the Array
constructor
As Array(1,2,3,4,5,6)
you can return an array of parameters passed. It Array.apply(thisArg,arguments)
can also be arguments
converted into an array, it really is possible experiments.
There is no effect, that is, the indiscriminate use of the constructor, but it is also characteristic of JS Well, the constructor is also a function.
Note that using this method: if an array within the array, will be assembled into an array.
The reason is that apply
parameter passing features.
4. cycle
Because arguments
similar arrays can be used arguments[0]
to access the argument, then assign each to a new array each direct copy than push
to be fast, if there is a function or an object arguments, it is necessary a deep copy.
Third, sum up
The most perfect method should be Array.prototype.slice.apply(arguments)
a.