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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27661306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:29:03  来源:igfitidea点击:

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

javascriptecmascript-6generatorarrow-functions

提问by Ashley Coolman

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)

采纳答案by Ashley Coolman

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

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

You can't. Sorry.

你不能。对不起。

According to MDN

根据MDN

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

function*声明(function关键字后跟一个星号)定义的发电机的功能。

From a spec document(my emphasis):

规范文件(我的重点):

The functionsyntax is extended to add an optional *token:

函数的语法扩展到添加一个可选的*令牌:

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

回答by CoderPi

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.

首先,箭头函数() => {}不是用来代替内联函数的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.

相比函数表达式的箭头函数表达式(也称为箭头功能)具有较短的语法,并且不结合其自身的thisargumentssuper,或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

The yieldkeyword 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 generatorswithout yielddon't make sense.

请注意,没有的生成器没有yield意义。



Why Arrow-function can not use yield

为什么箭头函数不能使用yield

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

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

Arrow functions bind thislexically, bind returnin the Blockbody case so it returns from the immediately enclosing arrow function, and preclude breakand continuefrom referencing statements outside the immediately enclosing arrow function.

The Identifierprimary expression argumentsmay not be used in an arrow function's body (whether expression or block form).

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

箭头函数在this词法上绑定return,在Block主体情况下绑定,以便它从直接封闭的箭头函数返回,并排除breakcontinue引用直接封闭的箭头函数之外的语句。

标识符初级表达arguments可能无法在箭头函数体被使用(是否表达或块形式)。

同样,yield不能在箭头函数的主体中使用。箭头不能是生成器,我们不想要深度延续。

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

箭头函数中的产量将引发语义错误: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# 也不允许这样做。

回答by monk-time

In addition to the discussion on esdiscuss.organd the Ecma TC39 committee ES6 meeting notes from November 2013mentioned 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:

除了上面提到的esdiscuss.org上的讨论和2013 年 11 月的 Ecma TC39 委员会 ES6 会议记录之外,在 2016 年 9 月的两次 ES7 会议中重新讨论了生成器箭头[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
  • 委员会有一些兴趣,但担心该功能不会为添加新语法而发挥作用
  • =>*作为 [Domenic Denicola] 异步迭代提案的一部分,计划在第 3 天重新访问,看看我们是否可以至少达到第 0 阶段

The proposal for generator arrows was moved to Stage 1with Brendan Eich and Domenic Denicola as champions. Asynchronous iterationmentioned above was finished and implementedin 2018.

发电机箭头的提议被移至第一阶段,Brendan Eich 和 Domenic Denicola 作为冠军。上述异步迭代于2018年完成并实现

In Oct 2019 an official repo by Sergey Rubanovappeared with more discussion about syntax and other details.

2019 年 10 月,谢尔盖·鲁巴诺夫 (Sergey Rubanov) 的官方回购出现了更多关于语法和其他细节的讨论。

回答by Bhojendra Rauniyar

I was also having the same question and came here. After reading the posts and comments, I felt using generator in an arrow function seems to be vague:

我也有同样的问题,来到这里。阅读帖子和评论后,我觉得在箭头函数中使用生成器似乎很模糊:

const generator = () => 2*3; // * implies multiplication
// so, this would be a confusing
const generator = () =>* something; // err, multiplying?
const generator = () =*> ... // err, ^^
const generator = ()*=> ... // err, *=3, still multiplying?
const generator=*()=> ... // err, ^^
const generator = *param => ... //err, "param" is not fixed word

This is what may be the big reason they didn't implement generator in relation with arrow function.

这可能是他们没有实现与箭头函数相关的生成器的重要原因。



But, if I were one of them, I could have thought like this:

但是,如果我是其中之一,我可能会这样想:

const generator = gen param => ... // hmm, gen indicates a generator
const generator = gen () => ... // ^^

This feels just like we have asynchronous function:

这感觉就像我们有异步函数:

const asyncFunction = async () => ... // pretty cool

Because, with normal function the asynckeyword exist, so arrow function is utilizing it - async () =>is likely to seem async function().

因为,在普通函数中存在async关键字,所以箭头函数正在使用它 -async () =>可能看起来是async function()

But, there's no keyword like genor generatorand alas arrow function is not using it.

但是,没有像genor这样的关键字generator,唉,箭头函数没有使用它。

To conclude:

总结:

Even if they wish to implement the generator in the arrow function, I think they need to re-think about generator syntax in core js:

即使他们希望在箭头函数中实现生成器,我认为他们需要重新考虑核心js中的生成器语法:

generator function myfunc() {}
// rather than
function* myfunc() {} // or, function *myfunc() {}

And this will be a big blunder. So, keeping arrow function out from the generator, is pretty cool.

这将是一个大错误。因此,将箭头函数排除在生成器之外,非常酷。



Following @Bergi comment:

以下@Bergi 评论

No. Arrow functions are supposed to be light-weight (and don't have a .prototype for example) and often one-liners, while generators are pretty much the opposite.

不。箭头函数应该是轻量级的(例如没有 .prototype)并且通常是单行的,而生成器则恰恰相反。

I will say that generator purpose to use is run-stop-runand so I don't think we need to care about prototype, lexical this, etc.

我会说生成器的使用目的是运行-停止-运行,所以我认为我们不需要关心原型、词法 this 等。

回答by coolreader18

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 ** () => {})?那是箭头函数的NaN9 次幂,返回,还是生成器箭头函数的 9 倍,也返回NaN?它可以使用一些替代语法来完成,就像=>*这里的另一个答案所提到的那样,但也许希望在实现时保持生成器函数语法(例如function* () {}and { *genMethod() {} })的一致性。不是太多的借口,而是一个理由。

回答by Gourav Makhija

Right now you can not, but in future you might be because TC39 release proposalfor same in october 2019, which is in stage 1.

现在你不能,但将来你可能会因为2019 年 10 月的TC39 发布提案,这是在第 1 阶段。

回答by Julius Baltru?aitis

There is a nice workaround with redux-saga

redux-saga 有一个很好的解决方法

import { call, all } from 'redux-saga/effects';

function* gen() {
   yield all([].map(() => {
      return call(....);
   }));
}