gpt4 book ai didi

javascript - 生成器函数 - = 上的星号是什么意思?

转载 作者:行者123 更新时间:2023-11-30 16:03:52 26 4
gpt4 key购买 nike

我正在浏览 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com