Skip to content
← Back to rules

eslint/no-iterator 正确性

This rule is turned on by default.
💡 A suggestion is available for this rule.

作用

禁止使用 __iterator__ 属性。

为什么这不好?

__iterator__ 属性是 JavaScript 的一个 SpiderMonkey 扩展, 可用于创建与 JavaScript 的 for in 和 for each 构造兼容的自定义迭代器。 然而,这个属性现在已经过时,因此不应再使用。下面是它 过去的工作方式示例:

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

示例

以下是此规则的错误代码示例:

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

foo.__iterator__ = function () {};

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

以下是此规则的正确代码示例:

js
const __iterator__ = 42; // 未使用 __iterator__ 属性

Foo.prototype[Symbol.iterator] = function () {
  return new FooIterator(this);
};

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-iterator": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-iterator": "error",
  },
});
bash
oxlint --deny no-iterator

版本

此规则添加于 v0.2.15。

参考资料