它是否传播“语法"?还是传播的“运营商"? [英] Is it spread "syntax" or the spread "operator"?

查看:107
本文介绍了它是否传播“语法"?还是传播的“运营商"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说...被称为传播语法"和传播 operator ",后者更为流行.相关的 MDN文档的网址建议它最初被称为传播 operator ,但后来更改为传播语法,并且

I've heard ... referred to both as 'spread syntax' and 'the spread operator', with the latter being a lot more popular. The URL of the relevant MDN documentation suggests that it was initially referred to as the spread operator but later changed to spread syntax, and MDN's list of operators doesn't mention it.

Google似乎建议使用术语 operator 更为流行和接受,例如 es6-features.org 这样引用.

Google seems to suggest the term operator is more popular and accepted, with sites such as the Microsoft documentation and es6-features.org referring to it as such.

在ECMAScript的上下文中,哪个术语是最正确的,为什么?数组销毁分配又如何呢?

Which term would be the most correct in the context of ECMAScript, if any, and why? What about array destructuring assignment?

推荐答案

它不是运算符.

从所有意义上说,它都不是一个.自引入以来,这一直是一个巨大的误解,尽管受到了公众的欢迎-这不是一个错误,并且有一些客观要点:

It's not an operator.

In all senses of the word, it's not one. It has been a huge misconception since it was introduced and despite popular opinion -- it's not one, and there are a few objective points to be made:

  • 它不符合运算符的定义
  • 它不能用作运算符
  • 语言规范暗示它不是运算符
  • It doesn't fit the definition of an operator
  • It can't be used as an operator
  • The language specification implies that it's not an operator

应该提到的是,传播语法具有不同的风味",在不同的上下文中使用,并且在使用相同的标点符号时通常以不同的名称进行引用.传播语法基本上是...标点符号应用的总称,请参阅 Felix Kling 的详细解答所有用途和名称.有关这些个人用途的更多说明,请参见补充答案.

It should be mentioned that spread syntax comes in different 'flavors', used in different contexts and are commonly referred to by different names while using the same punctuator. Spread syntax is basically an umbrella term for the application of the ... punctuator, and see Felix Kling's great answer detailing all the uses and names. More explanation about these individuals uses is given in the supplementary answer.

在ECMAScript上下文中,运算符只是内置函数,它们接受参数并求值单个值-以前缀,中缀或后缀表示法编写,通常使用符号名称,例如作为+/.来自维基百科:

Semantically, in the context of ECMAScript, operators are just builtin functions that take in arguments and evaluate to a single value -- written in prefix, infix, or postfix notation and usually with symbolic names such as + or /. From Wikipedia:

简而言之,以某种方式对涉及运算符的表达式进行求值,结果值可能只是一个值(一个r值),或者可能是一个允许赋值的对象(一个l值).

Simply, an expression involving an operator is evaluated in some way, and the resulting value may be just a value (an r-value), or may be an object allowing assignment (an l-value).

例如,+运算符的结果为2,它是右侧表达式,而.运算符的结果为对象允许赋值,例如foo.bar,即左侧表达式.手势.

For example, the + operator results in a value such as 2, which is a right-hand-side expression, and the . operator results in an object allowing assignment such as foo.bar, a left-hand-side expression.

表面上,...标点符号 1 看起来像是前缀一元运算符:

On the surface, the ... punctuator1 looks to be a prefix unary operator:

const baz = [foo, ...bar];

但是该参数的问题在于...bar的求值结果不是奇异;它将可迭代的bar元素一一展开.传播参数也是如此:

But the problem with that argument is that ...bar doesn't evaluate to a singular value; it spreads the iterable bar's elements, one by one. The same goes for spread arguments:

foo(...bar);

在这里,foo从可迭代的bar接收单独的参数.它们是传递给foo的单独值,而不仅仅是一个值.它不符合运算符的定义,因此不是一个.

Here, foo receives separate arguments from the iterable bar. They're separate values being passed to foo, not just one value. It doesn't fit the definition of an operator, so it isn't one.

要说明的另一点是,运算符应独立存在并返回单个值.例如:

Another point to be made is that operators are meant to be standalone and return a single value. For example:

const bar = [...foo];

如前所述,这很好.当您尝试执行此操作时会出现问题:

As already mentioned, this works well. The problem arises when you try to do this:

const bar = ...foo;

如果扩展语法是一个运算符,则后者会很好地运行,因为运算符将表达式评估为单个值,但扩展不会这样做,因此失败.扩展语法和扩展参数仅在数组和函数调用的上下文中起作用,因为这些结构会接收由扩展数组元素或参数提供的多个值.评估多个值超出了操作员的能力范围.

If spread syntax were an operator, the latter would work fine because operators evaluate the expression to a single value but spread doesn't so it fails. Spread syntax and spread arguments only work in the context of arrays and function calls because those structures receive multiple values supplied by spreading array elements or arguments. Evaluating to multiple values is outside of the scope of what an operator is able to do.

运算符的完整列表在 ECMAScript中的第12.5至§12.15条中列出. 2015语言规范,其中引入了...的规范,其中未提及....也可以推断出它不是运算符.此答案中提到的两种主要情况,即扩展语法出现在生产环境中,用于函数调用(扩展参数)或

The complete list of operators is listed in Clauses §12.5 through §12.15 in the ECMAScript 2015 Language Specification, the specification in which ... is introduced, which doesn't mention .... It can also be inferred that it's not an operator. The two main cases mentioned in this answer in which spread syntax is in a production, for function calls (spread arguments) or array literals (spread syntax) are described below:

ArrayLiteral :
  [ Elisionopt ]
  [ ElementList ]
  [ ElementList , Elisionopt ]

ElementList :
  Elisionopt AssignmentExpression
  Elisionopt SpreadElement
  ElementList , Elisionopt AssignmentExpression
  ElementList , Elisionopt SpreadElement

Elision :
  ,
  Elision ,

SpreadElement :
  ... AssignmentExpression
  

对于函数调用:

CallExpression :
  MemberExpression Arguments

Arguments :
  ( )
  ( ArgumentList )

ArgumentList :
  AssignmentExpression
  ... AssignmentExpression
  ArgumentList , AssignmentExpression
  ArgumentList , ... AssignmentExpression
  

在这些作品中,可以得出一个结论:传播的经营者"不存在.如前所述,运算符应独立于const bar = ...foo,并求值为一个单一值.该语言的语法阻止了这种情况,这意味着散布语法从来都不是独立的.它是数组初始化程序和函数调用的扩展,是它们语法的扩展.

In these productions, there's a conclusion that can be made: that the spread 'operator' doesn't exist. As mentioned earlier, operators should be standalone, as in const bar = ...foo and evaluate to one single value. The syntax of the language prevents this, which means spread syntax was never meant to be standalone. It's an extension to array initializers and function calls, an extension to their grammar.

语法,由维基百科定义:

在计算机科学中,计算机语言的语法是一组规则,用于定义符号组合,这些符号被认为是该语言的正确结构化文档或片段.

In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language.

语法基本上是语言的形式",用于控制有关法律外观和代码编写方式的合法性或不合法性的规则.在这种情况下,ECMAScript的语法专门定义了...标点符号,使其仅在函数调用和数组文字中作为扩展出现-这是一个规则,用于定义被视为合法的符号(...foo)的组合,因此,它的语法类似于箭头函数(=>)不是运算符,而是语法 2 .

Syntax is basically the 'form' of the language, rules that govern what is legal or not regarding how the code should look, and how the code should be written. In this case, ECMAScript's grammar specifically defines the ... punctuator to only appear in function calls and array literals as an extension -- which is a rule that defines a combination of symbols (...foo) that are considered to be legal together, thus it is syntax similar to how an arrow function (=>) is not an operator, but syntax2.

呼叫...操作员是用词不当.运算符是一个内置函数,它接受参数(操作数),并采用前缀,中缀或后缀表示形式,并且其计算结果恰好是一个值. ...,虽然满足前两个条件,但不满足最后一个条件.相反,...是语法,因为它是在语言的语法中明确定义的.因此,散布算子"在客观上更正确地称为散布语法".

Calling ... an operator is a misnomer. An operator is a builtin function that takes in arguments (operands) and is in the form of prefix, infix, or postfix notation and evaluates to exactly one value. ..., while satisfying the first two conditions, does not satisfy the last. ..., instead, is syntax because it is defined specifically and explicitly in the language's grammar. Thus, 'the spread operator' is objectively more correctly referred to as 'spread syntax'.

1 术语标点符号"是指

1 The term 'punctuator' refers to punctuators in ECMAScript 2015 and later specifications. These symbols include syntax components and operators, and are punctators of the language. ... is a punctuator itself, but the term 'spread syntax' refers to the whole application of the punctuator.

2 =>本身是一个标点符号,就像...一样,但是我具体指的是箭头函数语法=>标点符号((…) => { … })的应用与扩展语法一样,指的是...标点符号的应用.

2 => itself is a punctuator, just as ... but what I'm referring to specifically is arrow function syntax, the application of the => punctuator ((…) => { … }), just as spread syntax refers to the application of the ... punctuator.

这篇关于它是否传播“语法"?还是传播的“运营商"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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