在Javascript中何时执行yield语句?

在Javascript中何时执行yield语句?,javascript,generator,yield,Javascript,Generator,Yield,我对JS1.7新的收益特性中何时产生一个值感到有点困惑 当我这样编写函数时: function helloWorld() { console.log('hello'); yield "world"; } var sayHello = helloWorld(); sayHello.next(); function helloWorld() { console.log('hello'); yield console.log("world"); } var

我对JS1.7新的收益特性中何时产生一个值感到有点困惑

当我这样编写函数时:

function helloWorld() {
    console.log('hello'); 
    yield "world";
} 

var sayHello = helloWorld();

sayHello.next();
function helloWorld() {
    console.log('hello'); 
    yield console.log("world");
}

var sayHello = helloWorld();

sayHello.next();
它返回:

>"world"
>"hello"
>"hello"
>"world"
但当我这样写函数时:

function helloWorld() {
    console.log('hello'); 
    yield "world";
} 

var sayHello = helloWorld();

sayHello.next();
function helloWorld() {
    console.log('hello'); 
    yield console.log("world");
}

var sayHello = helloWorld();

sayHello.next();
它返回:

>"world"
>"hello"
>"hello"
>"world"
正如我所料


我很困惑,为什么在第一种情况下,“世界”在“你好”之前返回,也许我不明白收益率是如何工作的,有谁知道收益率为什么会这样做吗?

对不起,盖伊,这似乎是一个错误

我尝试使用Firefox控制台使用yield,它输出了这个结果,但在我实际构建了一个实际的文件并通过Firefox运行它之后,它的行为与预期的一样

如果有人好奇

<html>
<head>
</head>
<body>
<script type="application/javascript;version=1.7" >
function helloWorld () {
    console.log("hello"); 
    yield "world";
    yield "END";
} 

var sayHello = helloWorld();

console.log(sayHello.next());

function helloWorld1 () {
    console.log('hello'); 
    yield console.log("world");
    yield "END2";
}

var sayHello1 = helloWorld1();

sayHello1.next();
console.log(sayHello.next());
console.log(sayHello1.next());
</script>
</body>
</html>

函数helloWorld(){
console.log(“你好”);
产生“世界”;
产生“结束”;
} 
var sayHello=helloWorld();
log(sayHello.next());
函数helloWorld1(){
log('hello');
收益率控制台.log(“世界”);
产量“END2”;
}
var sayHello1=helloWorld1();
sayHello1.next();
log(sayHello.next());
log(sayHello1.next());
产出:

“你好” “世界” “你好” “世界” “结束”
“END2”

请看这里,这可能会回答您的问题。啊,我在开始使用收益率报表时确实先读过这篇文章,它从本质上比较了收益率和回报率。但如果我用return替换世界收益率,它仍然会在返回“world”之前记录“hello”。我想知道为什么在调用next时,如果它是一个原语,收益率值返回到“world”之前?我非常熟悉python中的生成器和屈服,但在新的JS实现中这似乎很奇怪。