2016-01-21 63 views
4

我在Mozilla Dev頁面查看生成器函數。關於生成器函數send()javascript

有一個示例代碼,它具有send()函數。

function* fibonacci() { 
    var a = yield 1; 
    yield a * 2; 
} 

var it = fibonacci(); 
console.log(it);   // "Generator { }" 
console.log(it.next()); // 1 
console.log(it.send(10)); // 20 
console.log(it.close()); // undefined 
console.log(it.next()); // throws StopIteration (as the generator is now closed) 

但是,Chrome和Firefox(最新版本)都在send()函數上拋出錯誤。

對此有何看法?它不支持?

+0

我記得,只有有限的瀏覽器支持ES6,你最好使用像transpilers BabelJS –

+0

在ES6,有隻有你傳遞值的'next'方法。你在看什麼頁面?請鏈接它,以便我們解決它(不要忘記MDN是一個wiki!) – Bergi

回答

2

.send是特定於SpiderMonkey引擎的Legacy generator objects的一部分。 It will be removed in some future release。他們已經開始刪除/替換在他們的代碼部分ES6發電機傳統發電機的對象(Bug 1215846Bug 1133277

對於時刻,你仍然可以使用傳統的發電機在Firefox(當前版本爲這個答案:43.0.4 )。在定義時,請忽略*,只要功能體使用yield語句,將使用傳統生成器。

function fibonacci() { 
    var a = yield 1; 
    yield a * 2; 
} 

var it = fibonacci(); 
console.log(it);   
console.log(it.next()); 
console.log(it.send(10)); 
console.log(it.close()); 
console.log(it.next()); 
+0

謝謝! @帕特里克埃文斯 – Vino

+0

謝謝! @Medet Tleukabiluly – Vino