Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is SpreadElement in ECMAScript documentation? Is it the same as Spread syntax at MDN?

At ECMAScript specification the SpreadElement is described

SpreadElement[Yield]: ...AssignmentExpression[In, ?Yield] 

Is this the same as the Spread syntax

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

Syntax

For function calls:

myFunction(...iterableObj); 

For array literals:

[...iterableObj, 4, 5, 6] 

described at MDN documentation?

What are use cases of SpreadElement and, or, spread syntax; and if SpreadElement and spread syntax are different, in which specific manners do they differ?

like image 767
guest271314 Avatar asked May 11 '16 02:05

guest271314


People also ask

What is spread syntax?

Spread syntax ( ... ) allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.

What is the difference between spread and rest operator?

The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaScript array. But the spread syntax expands iterables into individual elements.

What does spread mean in JavaScript?

The JavaScript spread operator ( ... ) allows us to quickly copy all or part of an existing array or object into another array or object.

What is the alternative for spread operator in JavaScript?

In a simple example code, the spread operator and the Object assign function both will take the right objects. There isn't a simple one to one mapping between using the spread operator and using apply .


1 Answers

The term "spread operator" is kind of an "umbrella term" that refers to various syntactic constructs in ES6 which all look like ...x. MDN does the same.

However, this is misguiding, because ... is not an operator (at least not in the sense the ECMAScript spec uses the term "operator"). It doesn't generate a value that can be used in further computations. I'd rather compare it to other punctuators, such as , or ; (which are also kind of related but have different meaning in different context).

The term "spread operator" could refer to:

  • Spread element, var arr = [a, b, ...c];: The spread element expands the iterable (c) into the new array. It's equivalent to something like [a,b].concat(c).

  • Rest element, [a, b, ...c] = arr;: Inside destructuring, the ... construct has the opposite effect: It collects remaining elements into an array. The example here is equivalent to

    a = arr[0]; b = arr[1]; c = arr.slice(2); 

    (note that this only an approximation, because destructuring works on any iterable value, not just arrays)

  • fun(a, b, ...c): This construct doesn't actually have a name in the spec. But it works very similar as spread elements do: It expands an iterable into the list of arguments.
    It would be equivalent to func.apply(null, [a, b].concat(c)).

    The lack of an official name might be the reason why people started to use "spread operator". I would probably call it "spread argument".

  • Rest parameter: function foo(a, b, ...c): Similar like rest elements, the rest parameter collects the remaining arguments passed to the function and makes them available as array in c. The ES2015 actually spec uses the term BindingRestElement to refer to to this construct.

Related questions:

  • Spread operator vs Rest parameter in ES2015/es6
  • Using spread operator multiple times in javascript?

: If we are very pedantic we would even have to distinguish between a variable declaration (var [a, b, ...c] = d;) and simple assignment ([a, b, ...c] = d;), according to the spec.

like image 160
Felix Kling Avatar answered Sep 30 '22 14:09

Felix Kling