我可以在生成器中使用ES6的箭头函数语法吗?(箭头符号)

时间:2021-09-26 15:15:29

ie how do I express this:

我该如何表达:

function *(next) {}

with arrows. I've tried all the combinations I could think of, and I can't find any documentation on it.

用箭头。我尝试了所有我能想到的组合,但是我找不到任何关于它的文档。

(currently using node v0.11.14)

(目前使用节点v0.11.14)

4 个解决方案

#1


125  

Can I use ES6's arrow function syntax with generators?

我可以在生成器中使用ES6的箭头函数语法吗?

You can't. Sorry.

你不能。对不起。

According to MDN

根据MDN

The function* statement (function keyword followed by an asterisk) defines a generator function.

函数*语句(函数关键字后面是星号)定义了一个生成器函数。

From a spec document (my emphasis):

从规范文件(我的重点):

The function syntax is extended to add an optional * token:

扩展函数语法,添加可选的*令牌:

FunctionDeclaration: "function" "*"? Identifier "(" FormalParameterList? ")" 
  "{" FunctionBody "}"

#2


82  

The difference between Inline-functions and Arrow-functions

First of all Arrow-functions () => {} are not made to replace Inline-functions function(){} and they are different. Inline-Functions are simply Functions, so the question is what the difference between Arrow-functions and Inline-Functions are.

首先,所有的arrow -function() =>{}都不是用来替换inline -function(){}的,它们是不同的。内联函数是简单的函数,所以问题是箭头函数和内联函数的区别是什么。

An arrow function expression (also known as arrow function) has a shorter syntax compared to function expressions and does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.

与函数表达式相比,箭头函数表达式(也称为箭头函数)具有更短的语法,并且不绑定自己的this、parameters、super或new.target)。箭头函数总是匿名的。

Some more quick details here

这里有一些更快捷的细节。


Why Arrow-function can not be used as generators

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Use of the yield keyword

使用yield关键字

The yield keyword may not be used in an arrow function's body (except when permitted within functions further nested within it). As a consequence, arrow functions cannot be used as generators.

yield关键字不能在箭头函数的主体中使用(除非在进一步嵌套的函数中允许使用)。因此,箭头函数不能用作生成器。

Note that generators without yield don't make sense.

注意,没有屈服的发电机没有意义。


Why Arrow-function can not use yield

http://tc39wiki.calculist.org/es6/arrow-functions/

http://tc39wiki.calculist.org/es6/arrow-functions/

Arrow functions bind this lexically, bind return in the Block body case so it returns from the immediately enclosing arrow function, and preclude break and continue from referencing statements outside the immediately enclosing arrow function.

箭头函数绑定这个词法,绑定返回在块体的情况下,使它从立即封闭的箭头函数返回,并排除中断,并继续引用在立即封闭的箭头函数之外的语句。

The Identifier primary expression arguments may not be used in an arrow function's body (whether expression or block form).

在箭头函数的主体(无论是表达式还是块形式)中,标识符主表达式参数不能使用。

Likewise, yield may not be used in an arrow function's body. Arrows cannot be generators and we do not want deep continuations.

同样,屈服也不能用于箭头函数的主体。箭头不能是生成器,我们不希望有深度连续。

Yield in an Arrow-Function will throw Semantic Error: http://www.ecma-international.org/

Arrow-Function中的Yield将抛出语义错误:http://www.ecma-international.org/

In the End the reason is in the deep complexity in the implementation of ECMA6. C# does not allow this as well for somewhat similar reasons.

最后,原因在于ECMA6的实现非常复杂。由于类似的原因,c#也不允许这样做。

#3


21  

In addition to the discussion on esdiscuss.org and the Ecma TC39 committee ES6 meeting notes from November 2013 mentioned above, generator arrows were revisited in two September 2016 ES7 meetings [1] [2]. After a discussion about pros and cons of various syntax (mainly =*> and =>*) and a lack of justifications and use cases for this feature, they came to the conclusion that:

除了以上提到的2013年11月关于esdiscuss.org和Ecma TC39委员会ES6会议说明的讨论外,2016年9月两次esdiscussion s7会议[1][2]重新访问了发电机箭头。在讨论了各种语法的优缺点(主要是=*>和=>*)以及对该特性缺乏理由和用例之后,他们得出的结论是:

  • There is some interest from the committee, but concern that the feature does not pull its weight for adding a new piece of syntax
  • 委员会对此有些兴趣,但担心该特性不会对添加新语法起作用
  • Plan to revisit on Day 3 to see if we can get =>* to stage 0 at least, as part of [Domenic Denicola]'s async iteration proposal
  • 计划在第3天重访,看看我们是否可以将=>*至少作为[Domenic Denicola]的异步迭代建议的一部分

The proposal for generator arrows was moved to Stage 1 with Brendan Eich and Domenic Denicola as champions, but a relevant tc39/proposals repo doesn't exist yet. I assume for further news one has to wait until the Stage 3 async iteration proposal is finalized.

发电机箭头的提议被转移到第1阶段,Brendan Eich和domaindenicola作为冠军,但是相关的tc39/提案还不存在。我认为,要获得更多的消息,必须等到第三阶段异步迭代建议完成之后。

#4


1  

I know that this is very late, but another possible reason could be syntax. maybe (*() => {}) works, but what about (9 ** () => {})? Is that 9 to the power of an arrow function, returning NaN, or is it 9 times a generator arrow function, also returning NaN? It could be done with some alternative syntax, like =>* as mentioned by another answer here, but maybe there was a desire to preserve the consistency of the generator function syntax (eg. function* () {} and { *genMethod() {} }) when it was being implemented. Not too much of an excuse, but a reason for it.

我知道这已经很晚了,但另一个可能的原因可能是语法。也许(*()= > { }),但(9 * *()= > { })?这个9的能量是一个箭头函数,返回NaN,还是它是9乘以一个生成器的箭头函数,也返回NaN?可以使用一些替代语法来完成,如这里的另一个答案所提到的=>*,但是可能存在保留生成器函数语法一致性的愿望(例如)。函数*(){}和{*genMethod(){}})在实现时。不是借口,而是理由。

#1


125  

Can I use ES6's arrow function syntax with generators?

我可以在生成器中使用ES6的箭头函数语法吗?

You can't. Sorry.

你不能。对不起。

According to MDN

根据MDN

The function* statement (function keyword followed by an asterisk) defines a generator function.

函数*语句(函数关键字后面是星号)定义了一个生成器函数。

From a spec document (my emphasis):

从规范文件(我的重点):

The function syntax is extended to add an optional * token:

扩展函数语法,添加可选的*令牌:

FunctionDeclaration: "function" "*"? Identifier "(" FormalParameterList? ")" 
  "{" FunctionBody "}"

#2


82  

The difference between Inline-functions and Arrow-functions

First of all Arrow-functions () => {} are not made to replace Inline-functions function(){} and they are different. Inline-Functions are simply Functions, so the question is what the difference between Arrow-functions and Inline-Functions are.

首先,所有的arrow -function() =>{}都不是用来替换inline -function(){}的,它们是不同的。内联函数是简单的函数,所以问题是箭头函数和内联函数的区别是什么。

An arrow function expression (also known as arrow function) has a shorter syntax compared to function expressions and does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.

与函数表达式相比,箭头函数表达式(也称为箭头函数)具有更短的语法,并且不绑定自己的this、parameters、super或new.target)。箭头函数总是匿名的。

Some more quick details here

这里有一些更快捷的细节。


Why Arrow-function can not be used as generators

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Use of the yield keyword

使用yield关键字

The yield keyword may not be used in an arrow function's body (except when permitted within functions further nested within it). As a consequence, arrow functions cannot be used as generators.

yield关键字不能在箭头函数的主体中使用(除非在进一步嵌套的函数中允许使用)。因此,箭头函数不能用作生成器。

Note that generators without yield don't make sense.

注意,没有屈服的发电机没有意义。


Why Arrow-function can not use yield

http://tc39wiki.calculist.org/es6/arrow-functions/

http://tc39wiki.calculist.org/es6/arrow-functions/

Arrow functions bind this lexically, bind return in the Block body case so it returns from the immediately enclosing arrow function, and preclude break and continue from referencing statements outside the immediately enclosing arrow function.

箭头函数绑定这个词法,绑定返回在块体的情况下,使它从立即封闭的箭头函数返回,并排除中断,并继续引用在立即封闭的箭头函数之外的语句。

The Identifier primary expression arguments may not be used in an arrow function's body (whether expression or block form).

在箭头函数的主体(无论是表达式还是块形式)中,标识符主表达式参数不能使用。

Likewise, yield may not be used in an arrow function's body. Arrows cannot be generators and we do not want deep continuations.

同样,屈服也不能用于箭头函数的主体。箭头不能是生成器,我们不希望有深度连续。

Yield in an Arrow-Function will throw Semantic Error: http://www.ecma-international.org/

Arrow-Function中的Yield将抛出语义错误:http://www.ecma-international.org/

In the End the reason is in the deep complexity in the implementation of ECMA6. C# does not allow this as well for somewhat similar reasons.

最后,原因在于ECMA6的实现非常复杂。由于类似的原因,c#也不允许这样做。

#3


21  

In addition to the discussion on esdiscuss.org and the Ecma TC39 committee ES6 meeting notes from November 2013 mentioned above, generator arrows were revisited in two September 2016 ES7 meetings [1] [2]. After a discussion about pros and cons of various syntax (mainly =*> and =>*) and a lack of justifications and use cases for this feature, they came to the conclusion that:

除了以上提到的2013年11月关于esdiscuss.org和Ecma TC39委员会ES6会议说明的讨论外,2016年9月两次esdiscussion s7会议[1][2]重新访问了发电机箭头。在讨论了各种语法的优缺点(主要是=*>和=>*)以及对该特性缺乏理由和用例之后,他们得出的结论是:

  • There is some interest from the committee, but concern that the feature does not pull its weight for adding a new piece of syntax
  • 委员会对此有些兴趣,但担心该特性不会对添加新语法起作用
  • Plan to revisit on Day 3 to see if we can get =>* to stage 0 at least, as part of [Domenic Denicola]'s async iteration proposal
  • 计划在第3天重访,看看我们是否可以将=>*至少作为[Domenic Denicola]的异步迭代建议的一部分

The proposal for generator arrows was moved to Stage 1 with Brendan Eich and Domenic Denicola as champions, but a relevant tc39/proposals repo doesn't exist yet. I assume for further news one has to wait until the Stage 3 async iteration proposal is finalized.

发电机箭头的提议被转移到第1阶段,Brendan Eich和domaindenicola作为冠军,但是相关的tc39/提案还不存在。我认为,要获得更多的消息,必须等到第三阶段异步迭代建议完成之后。

#4


1  

I know that this is very late, but another possible reason could be syntax. maybe (*() => {}) works, but what about (9 ** () => {})? Is that 9 to the power of an arrow function, returning NaN, or is it 9 times a generator arrow function, also returning NaN? It could be done with some alternative syntax, like =>* as mentioned by another answer here, but maybe there was a desire to preserve the consistency of the generator function syntax (eg. function* () {} and { *genMethod() {} }) when it was being implemented. Not too much of an excuse, but a reason for it.

我知道这已经很晚了,但另一个可能的原因可能是语法。也许(*()= > { }),但(9 * *()= > { })?这个9的能量是一个箭头函数,返回NaN,还是它是9乘以一个生成器的箭头函数,也返回NaN?可以使用一些替代语法来完成,如这里的另一个答案所提到的=>*,但是可能存在保留生成器函数语法一致性的愿望(例如)。函数*(){}和{*genMethod(){}})在实现时。不是借口,而是理由。