]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-iterator | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | further_reading: | |
5 | - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators | |
6 | - https://kangax.github.io/es5-compat-table/es6/#Iterators | |
7 | - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#Object_methods | |
8 | --- | |
9 | ||
eb39fafa DC |
10 | |
11 | 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: | |
12 | ||
13 | ```js | |
14 | Foo.prototype.__iterator__ = function() { | |
15 | return new FooIterator(this); | |
16 | } | |
17 | ``` | |
18 | ||
19 | You should use ECMAScript 6 iterators and generators instead. | |
20 | ||
21 | ## Rule Details | |
22 | ||
23 | 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. | |
24 | ||
25 | Examples of **incorrect** code for this rule: | |
26 | ||
8f9d1d4d DC |
27 | ::: incorrect |
28 | ||
eb39fafa DC |
29 | ```js |
30 | /*eslint no-iterator: "error"*/ | |
31 | ||
32 | Foo.prototype.__iterator__ = function() { | |
33 | return new FooIterator(this); | |
34 | }; | |
35 | ||
36 | foo.__iterator__ = function () {}; | |
37 | ||
38 | foo["__iterator__"] = function () {}; | |
39 | ||
40 | ``` | |
41 | ||
8f9d1d4d DC |
42 | ::: |
43 | ||
eb39fafa DC |
44 | Examples of **correct** code for this rule: |
45 | ||
8f9d1d4d DC |
46 | ::: correct |
47 | ||
eb39fafa DC |
48 | ```js |
49 | /*eslint no-iterator: "error"*/ | |
50 | ||
51 | var __iterator__ = foo; // Not using the `__iterator__` property. | |
52 | ``` | |
53 | ||
8f9d1d4d | 54 | ::: |