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

标签 javascript ecmascript-6 generator

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

相关文章:

javascript - 如果导航到 React Native 中的其他屏幕,如何保留 formik 表单值

javascript - 使用模板字面量的动态 Vue 组件导入路径

javascript - 卸载组件时对 setState 发出警告

python - 在 xrange 对象上调用多个迭代器

python - 在 __iter__() 中使用 yield 有什么好处?

python - 在 python 函数调用的参数列表中嵌套生成器表达式

javascript - Bootstrap 中的切换按钮不想 "untoggle"

javascript - jQuery UI Sortable 工作异常

javascript - 删除挖空 View 模型的空属性

javascript - 了解 Javascript 中的 "in"关键字