是... foo一个操作符或语法? [英] Is ...foo an operator or syntax?

查看:132
本文介绍了是... foo一个操作符或语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经听到 ... 被称为扩展语法和传播操作符 后者比较受欢迎。相关 MDN文档的网址表明,它最初被称为传播操作符,但后来更改为扩展语法,而 MDN的运算符列表没有提到。

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似乎建议术语运算符更受欢迎并被接受,网站如 Microsoft文档 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

应该是提到传播语法有不同的风格,在不同的上下文中使用,并且在使用相同的标点符号时通常被不同的名称引用。 Spread语法基本上是应用 ... 标点符号的总称,见 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.

表面上, ... punctuator 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 单独接收来自可迭代的的参数。它们是单独的值传递给 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.

运营商的完整列表列在了中的§12.5至§12.15中ECMAScript 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.

语法,由维基百科定义: p>

Syntax, as defined by Wikipedia:


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

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 术语标点符号是指 ECMAScript 2015中的标点符号及以后的规格。这些符号包括语法组件和运算符,并且是语言的标点符号。 ... 是一个标点符号本身,但术语扩展语法是指标点符号的整个应用程序。

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 => 本身是一个标点符号,就像 ... 但是我特别指的是 arrow函数语法,应用程序的 => 标点符号((...)=> {...} ),就像扩展语法是指应用 ... punctuator。

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.

这篇关于是... foo一个操作符或语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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