Converted into an array of method arguments

The function in the argumentsconversion method into a real array.

argumentsIs 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.calleeis 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 argumentsobject.

A, argumentsit 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.lengthwhat is it?

    arguments.calleeIt is a reference to the function currently being performed, similar to function.lengththat parameter number.

Second, the argumentsconversion 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 argumentsobject has lengthproperties, and this method will be based on lengthattributes, have a return lengtharray length. If the lengthproperty is not number, it returns the length of the array 0;

So long as the other objects lengthproperties are also possible yo, as the object has attributes 0, it is the corresponding arr[0], i.e., property is a natural number numberthat 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)

thisArgThe new array is empty, applythe method will function thispoint thisArg, argumentsas a reference to the array-transmission apply.

According to applythe role of the method, it is about Array.prototype.slicethe method specified thisas thisArgcall 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 applyparameter passing features.

3. I thought of a method, using the Arrayconstructor

As Array(1,2,3,4,5,6)you can return an array of parameters passed. It Array.apply(thisArg,arguments)can also be argumentsconverted 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 applyparameter passing features.

4. cycle

Because argumentssimilar arrays can be used arguments[0]to access the argument, then assign each to a new array each direct copy than pushto 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.

Guess you like

Origin blog.csdn.net/weixin_33912453/article/details/91364801