Index

no-iterator

禁止使用 __iterator__ 属性

__iterator__ 属性是 SpiderMonkey 对 JavaScript 的扩展,可用于创建与 JavaScript 的 for infor each 构造兼容的自定义迭代器。然而,该属性现在已过时,因此不应使用。以下是它过去如何工作的示例:

🌐 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:

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

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

🌐 You should use ECMAScript 6 iterators and generators instead.

规则详情

🌐 Rule Details

此规则旨在防止由于使用 __iterator__ 属性而可能出现的错误,该属性在多个浏览器中尚未实现。因此,每当遇到 __iterator__ 属性时,它都会发出警告。

🌐 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.

此规则的错误代码示例:

🌐 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"*/

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

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

版本

此规则是在 ESLint v0.0.9 中引入。

进阶读物

资源