no-iterator
禁止使用 __iterator__
属性
__iterator__
属性是 JavaScript 的 SpiderMonkey 扩展,可用于创建与 JavaScript 的 for in
和 for 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"*/
var __iterator__ = foo; // Not using the `__iterator__` property.
版本
此规则是在 ESLint v0.0.9 中引入。