ECMAScript文档中的SpreadElement是什么?它与MDN上的Spread语法相同吗? [英] What is SpreadElement in ECMAScript documentation? Is it the same as Spread syntax at MDN?

查看:170
本文介绍了ECMAScript文档中的SpreadElement是什么?它与MDN上的Spread语法相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ECMAScript规范 SpreadElement 被描述

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

这是否与 Spread语法


Spread语法允许迭代,如数组表达式或要在需要零个或多个参数(用于函数调用)或元素(用于数组文字)的位置扩展的字符串,或者在需要零个或多个键值对(用于对象文字)的位置展开的对象表达式。

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.

语法

对于函数调用:

myFunction(...iterableObj);

对于数组文字:

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


MDN 文档中有描述?

SpreadElement 和/或传播语法的用例是什么?如果 SpreadElement 和传播语法有所不同,具体方式有何不同?

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?

推荐答案

术语传播运算符是一种总称术语,指的是ES6中的各种句法结构,它们都看起来像 ... x 。 MDN也是如此。

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.

然而,这是误导,因为 ... 不是运营商(至少在某种意义上,ECMAScript规范使用术语运算符)。它不会生成可用于进一步计算的值。我宁愿将它与其他标点符号进行比较,例如; (这些也是相关的,但有不同背景下的不同含义。)

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元素, [a,b, ... c] = arr; :内部解构,。 .. construct具有相反的效果:它将剩余的元素收集到一个数组中。这里的例子相当于

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) :这个构建实际上没有名称符合规范。但是它与spread元素的工作方式非常相似:它将一个iterable扩展为参数列表。

它等价于 func.apply(null,[a,b]。 concat(c))

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".

休息参数: function foo(a,b,... c) :与rest元素类似,rest参数收集传递给函数的其余参数,并使它们在 c 中作为数组使用。 ES2015实际规范使用术语 BindingRestElement 来引用此构造。

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.

相关问题:

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

:如果我们非常迂腐我们甚至要区分变量声明( var [a,b,... c] = d; )根据规范进行简单分配( [a,b,... c] = d; )。

: 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.

这篇关于ECMAScript文档中的SpreadElement是什么?它与MDN上的Spread语法相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆