我正在尝试从这里运行代码
function fib() {
var i = 0, j = 1;
while (true) {
yield i;
var t = i;
i = j;
j += t;
}
}
var g = fib();
for (var i = 0; i < 10; i++) {
console.log(g.next());
}
我无法让它在 Node.js、Chrome 或 Firefox 中运行
我正在尝试从这里运行代码
function fib() {
var i = 0, j = 1;
while (true) {
yield i;
var t = i;
i = j;
j += t;
}
}
var g = fib();
for (var i = 0; i < 10; i++) {
console.log(g.next());
}
我无法让它在 Node.js、Chrome 或 Firefox 中运行
It's an EcmaScript.next feature that is being tested in newer versions of JavaScript interpreters.
Mozilla's "Iterators and Generators" explains how to use them.
To see which browsers support which ES.next features, see kangax's compatibility chart and browser specific reports. Although Chrome as a whole does not yet, jmar777 reports that V8 supports it (as of Aug 2013).
If this code is executing in Node.js application on the server side it doesn't matter what browser you're using as this isn't the execution environment for that block of code - it's on your server and as far as i know Node.js is using a version of the V8 JS engine that doesn't yet have ES6 support.