为什么 yield 被归类为运算符而不是语句?
Why is yield classed as an operator and not a statement?
我在看 2 的 mdn javascript reference and noticed that yield
is listed in the operators section. On the other hand return
is listed as a statement. I also found yield
has a operator precedence。
yield
的哪些特征使其落入运算符 class 而不是语句?为什么 return
属于语句而不是运算符?
我不确定这一点,但在生成器的上下文中 yield
将数据发送到 generator.next()
的方式就像一个函数一样运行。运算符是大多数语言中的特殊 类 函数(包括 JavaScript)。
您几乎可以想象 generator.next
调用它的实例并传递有关恢复位置的回调。 yield
调用回调
Return
表示执行路径结束,并将 return 值替换到适当的内存位置并将调用堆栈展开 1 个单元。如果感觉原始的语言定义,
它是一个运算符,因为它可以用在表达式中。
function* g() {
value = 3;
while (value !== 5) value = Math.floor(yield value + 1);
}
var gen = g();
console.log(gen.next().value);
console.log(gen.next(1.5).value);
我在看 2 的 mdn javascript reference and noticed that yield
is listed in the operators section. On the other hand return
is listed as a statement. I also found yield
has a operator precedence。
yield
的哪些特征使其落入运算符 class 而不是语句?为什么 return
属于语句而不是运算符?
我不确定这一点,但在生成器的上下文中 yield
将数据发送到 generator.next()
的方式就像一个函数一样运行。运算符是大多数语言中的特殊 类 函数(包括 JavaScript)。
您几乎可以想象 generator.next
调用它的实例并传递有关恢复位置的回调。 yield
调用回调
Return
表示执行路径结束,并将 return 值替换到适当的内存位置并将调用堆栈展开 1 个单元。如果感觉原始的语言定义,
它是一个运算符,因为它可以用在表达式中。
function* g() {
value = 3;
while (value !== 5) value = Math.floor(yield value + 1);
}
var gen = g();
console.log(gen.next().value);
console.log(gen.next(1.5).value);