Disallow Iterator (no-iterator)

禁用迭代器 (no-iterator)

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:

__iterator__ 属性曾是 SpiderMonkey 对 JavaScript 的扩展,被用来创建自定义迭代器,兼容JavaScript的 for infor each。然而,这个属性现在废弃了,所以不应再使用它。这里有个例子,展示它是如何使用的:

Foo.prototype.__iterator__ = function() {
    return new FooIterator(this);
}

You should use ECMAScript 6 iterators and generators instead.

你应该使用 ECMAScript 6 迭代器和生成器。

Rule Details

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.

此规则目的在于防止因使用 __iterator__属性而出现的错误,并不是所有浏览器都实现了这个属性。因此,当遇到 __iterator__属性时,该规则将会发出警告。

Examples of incorrect code for this rule:

错误 代码示例:

/*eslint no-iterator: "error"*/

Foo.prototype.__iterator__ = function() {
    return new FooIterator(this);
};

foo.__iterator__ = function () {};

foo["__iterator__"] = function () {};

Examples of correct code for this rule:

正确 代码示例:

/*eslint no-iterator: "error"*/

var __iterator__ = foo; // Not using the `__iterator__` property.

Further Reading

Version

This rule was introduced in ESLint 0.0.9.

该规则在 ESLint 0.0.9 中被引入。

Resources