问题描述
在中被描述
SpreadElement[Yield]:
...AssignmentExpression[In, ?Yield]
这是否与
语法
对于函数调用:
myFunction(...iterableObj);
对于数组文字:
[...iterableObj, 4, 5, 6]
在文档中有描述?
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元素扩展了iterable(
c
)进入新数组。它等同于[a,b] .concat(c)
。
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)
.
:内部解构,。 ..
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".
:与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语法相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!