]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-iterator.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-iterator.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-iterator
8f9d1d4d
DC
3rule_type: suggestion
4further_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
11The `__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
14Foo.prototype.__iterator__ = function() {
15 return new FooIterator(this);
16}
17```
18
19You should use ECMAScript 6 iterators and generators instead.
20
21## Rule Details
22
23This 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
25Examples of **incorrect** code for this rule:
26
8f9d1d4d
DC
27::: incorrect
28
eb39fafa
DC
29```js
30/*eslint no-iterator: "error"*/
31
32Foo.prototype.__iterator__ = function() {
33 return new FooIterator(this);
34};
35
36foo.__iterator__ = function () {};
37
38foo["__iterator__"] = function () {};
39
40```
41
8f9d1d4d
DC
42:::
43
eb39fafa
DC
44Examples of **correct** code for this rule:
45
8f9d1d4d
DC
46::: correct
47
eb39fafa
DC
48```js
49/*eslint no-iterator: "error"*/
50
51var __iterator__ = foo; // Not using the `__iterator__` property.
52```
53
8f9d1d4d 54:::