]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-iterator.md
b0335e8fde31dfb8032e237cd79de39f799568a9
[pve-eslint.git] / eslint / docs / src / rules / no-iterator.md
1 ---
2 title: no-iterator
3 layout: doc
4 rule_type: suggestion
5 further_reading:
6 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
7 - https://kangax.github.io/es5-compat-table/es6/#Iterators
8 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#Object_methods
9 ---
10
11
12 The `__iterator__` property was a SpiderMonkey extension to JavaScript that could be used to create custom iterators that are compatible with JavaScript's `for in` and `for each` constructs. However, this property is now obsolete, so it should not be used. Here's an example of how this used to work:
13
14 ```js
15 Foo.prototype.__iterator__ = function() {
16 return new FooIterator(this);
17 }
18 ```
19
20 You should use ECMAScript 6 iterators and generators instead.
21
22 ## Rule Details
23
24 This rule is aimed at preventing errors that may arise from using the `__iterator__` property, which is not implemented in several browsers. As such, it will warn whenever it encounters the `__iterator__` property.
25
26 Examples of **incorrect** code for this rule:
27
28 ::: incorrect
29
30 ```js
31 /*eslint no-iterator: "error"*/
32
33 Foo.prototype.__iterator__ = function() {
34 return new FooIterator(this);
35 };
36
37 foo.__iterator__ = function () {};
38
39 foo["__iterator__"] = function () {};
40
41 ```
42
43 :::
44
45 Examples of **correct** code for this rule:
46
47 ::: correct
48
49 ```js
50 /*eslint no-iterator: "error"*/
51
52 var __iterator__ = foo; // Not using the `__iterator__` property.
53 ```
54
55 :::