Bản dịch này chưa hoàn thành. Xin hãy giúp dịch bài viết này từ tiếng Anh.

Generator là một object return bởi một generator function, nó phù hợp với cả iterable protocol và iterator protocol.

Cú pháp

function* gen() { 
  yield 1;
  yield 2;
  yield 3;
}

var g = gen(); // "Generator { }"

Phương thức

Generator.prototype.next()
Trả về giá trị yielded, được khai báo qua câu lệnh yield.
Generator.prototype.return()
Trả về giá trị và kết thúc generator.
Generator.prototype.throw()
Quăng lỗi vào generator (đồng thời kết thúc generator, trừ khi được bắt lại trong generator đó).

Ví dụ

Một vòng lặp vô hạn

function* idMaker() {
    var index = 0;
    while(true)
        yield index++;
}

var gen = idMaker(); // "Generator { }"

console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
// ...

Generator object cũ

Firefox (SpiderMonkey) đã hiện thực phiên bản generators đầu tiên trong JavaScript 1.7, lúc đó dấu sao (*) trong khai báo không bắt buộc (bạn chỉ cần dùng từ khóa yield bên trong hàm). Tuy nhiên, kiểu viết này đã không còn được hổ trợ từ Firefox 58 (released ngày 23, tháng 1, 2018) (bug 1083482).

Các phương thức generator cũ

Generator.prototype.next()
Returns a value yielded by the yield expression. This corresponds to next() in the ES2015 generator object.
Generator.prototype.close()
Closes the generator, so that when calling next() an StopIteration error will be thrown. This corresponds to the return() method in the ES2015 generator object.
Generator.prototype.send()
Used to send a value to a generator. The value is returned from the yield expression, and returns a value yielded by the next yield expression. send(x) corresponds to next(x) in the ES2015 generator object.
Generator.prototype.throw()
Throws an error to a generator. This corresponds to the throw() method in the ES2015 generator object.

Legacy generator example

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)

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Generator objects' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Generator objects' in that specification.
Draft  

Trình duyệt hổ trợ

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 39Edge Full support 13Firefox Full support 26IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 13Firefox Android Full support 26Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support Yesnodejs Full support 4.0.0
Full support 4.0.0
Full support 0.12
Disabled
Disabled From version 0.12: this feature is behind the --harmony runtime flag.
nextChrome Full support 39Edge Full support 13Firefox Full support 26IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 13Firefox Android Full support 26Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support Yesnodejs Full support Yes
returnChrome Full support 50Edge Full support 13Firefox Full support 38IE No support NoOpera Full support 37Safari Full support 10WebView Android ? Chrome Android Full support 50Edge Mobile Full support 13Firefox Android Full support 38Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.0.0
throwChrome Full support 39Edge Full support 13Firefox Full support 26IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 13Firefox Android Full support 26Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support Yesnodejs Full support 4.0.0
Full support 4.0.0
Full support 0.12
Disabled
Disabled From version 0.12: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
User must explicitly enable this feature.
User must explicitly enable this feature.

Xem thêm

Legacy generators

ES2015 generators

Document Tags and Contributors

Những người đóng góp cho trang này: luubinhan
Cập nhật lần cuối bởi: luubinhan,