我正在浏览 Nodeschool exercise on Generators .
第二个练习的答案是这样的:
function *factorial (n) {
var result = 1;
for (var i = 1; i <= n; i++) {
result *= i;
yield result;
}
}
for (var n of factorial(5)) {
console.log(n)
}
result *= i
是什么意思?
最佳答案
Object-Oriented JavaScript - Second Edition: There is also a family of operators that are a combination of an assignment and an arithmetic operator. These are called compound operators. They can make your code more compact.
让我们看看其中的一些示例:
var a = 5;
a = a + 3; // 8
更短的方式:
var a = 5;
a += 3; // 8
在您的代码中:
result *= i;
相当于:
result = result * i;
关于javascript - 生成器函数 - = 上的星号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37319425/