Using the spread operator with the Object Argument function

I know that you have to be very careful with the function Argument object

But are there any known flaws (optimization / performance issues) using the spread operator with the Arguments object? Or is it completely normal?

I want to create an array from an unknown number of arguments passed to the function:

function Numbers(){
    this.numbers = [...arguments];
}

The violin can be found here.

This looks pretty neat, and the MDN page about the Arguments object even suggests that I can use the spread operator to do this:

You can also use a method Array.from()or distribution operator to convert the arguments to a real array.

But I still would like to see if others have an opinion on this.

+4
4

:

function Numbers(...numbers){
    this.numbers = numbers;
}
+4

, ES5

this.numbers = [].slice.call(arguments);

ES2015

this.numbers = [...arguments];

, (no arguments), . Array.from - , .

+2

( ) arguments. , , , - arguments. ES2015 (, ), arguments.

, : rest:

function Numbers(...args){
    this.numbers = args;
}

args - . .

+1
source

One of the reasons people say not to use the arguments object is because it looks like an array, but it doesn't:

The arguments object is not an array. It looks like an array, but has no Array properties other than length. For example, it does not have a pop method. However, it can be converted to a real array:

You can go here because, for example, Array.from () the distribution operator creates a new copy

0
source

Source: https://habr.com/ru/post/1656247/


All Articles