Why is ES6 “yield” a reserved word when called in this context?

前端 未结 5 2134
别跟我提以往
别跟我提以往 2020-12-09 09:34

I am using node 4.1.1. When I run this code

\"use strict\";

function *generator() {
  let numbers = [1,2,3,4,5];
  numbers.map(n => yield (n + 1));
}

f         


        
5条回答
  •  悲&欢浪女
    2020-12-09 10:14

    It is because arrow functions are not generator functions. For example,

    function temp() {
      yield 1;
    }
    

    Can we expect this to work? No. Because temp is not a generator function. The same is applicable to arrow functions as well.


    FWIW, the usage of yield in an Arrow function is an early error as per the ECMAScript 2015 specification, as per this section,

    ArrowFunction : ArrowParameters => ConciseBody

    • It is a Syntax Error if ArrowParameters Contains YieldExpression is true.

    • It is a Syntax Error if ConciseBody Contains YieldExpression is true.

提交回复
热议问题