prefer-reflect
适用时需要 Reflect
方法
该规则在 ESLint v3.9.0 中已弃用,并且不会被替换。这条规则的初衷现在似乎被误导了,因为我们已经了解到 Reflect
方法实际上并不是要替换规则建议的 Object
对应物,而是作为底层基础类型存在,与代理一起使用以复制默认值各种先前存在的功能的行为。
¥This rule was deprecated in ESLint v3.9.0 and will not be replaced. The original intent of this rule now seems misguided as we have come to understand that Reflect
methods are not actually intended to replace the Object
counterparts the rule suggests, but rather exist as low-level primitives to be used with proxies in order to replicate the default behavior of various previously existing functionality.
请注意:此规则包含不正确的行为 - 它会建议你使用 Reflect.getOwnPropertyNames
而不是 Object.getOwnPropertyNames
,但前者在 规范 中不存在。我们建议将 exceptions
选项与 "getOwnPropertyNames"
一起使用,以避免这种错误建议。
¥Please note: This rule contains an incorrect behavior - it will suggest you to use Reflect.getOwnPropertyNames
rather than Object.getOwnPropertyNames
, but the former one doesn’t exist in the specification. We suggest using the exceptions
option with "getOwnPropertyNames"
to avoid this false suggestion.
ES6 Reflect API 附带了一些方法,它们在一定程度上弃用了旧构造函数的方法:
¥The ES6 Reflect API comes with a handful of methods which somewhat deprecate methods on old constructors:
-
Reflect.apply
有效地弃用了Function.prototype.apply
和Function.prototype.call
¥
Reflect.apply
effectively deprecatesFunction.prototype.apply
andFunction.prototype.call
-
Reflect.deleteProperty
有效地弃用了delete
关键字¥
Reflect.deleteProperty
effectively deprecates thedelete
keyword -
Reflect.getOwnPropertyDescriptor
有效地弃用了Object.getOwnPropertyDescriptor
¥
Reflect.getOwnPropertyDescriptor
effectively deprecatesObject.getOwnPropertyDescriptor
-
Reflect.getPrototypeOf
有效地弃用了Object.getPrototypeOf
¥
Reflect.getPrototypeOf
effectively deprecatesObject.getPrototypeOf
-
Reflect.setPrototypeOf
有效地弃用了Object.setPrototypeOf
¥
Reflect.setPrototypeOf
effectively deprecatesObject.setPrototypeOf
-
Reflect.preventExtensions
有效地弃用了Object.preventExtensions
¥
Reflect.preventExtensions
effectively deprecatesObject.preventExtensions
prefer-reflect 规则将标记任何旧方法的使用,建议改用较新的 Reflect 版本。
¥The prefer-reflect rule will flag usage of any older method, suggesting to instead use the newer Reflect version.
规则详情
¥Rule Details
选项
¥Options
异常
¥Exceptions
"prefer-reflect": [<enabled>, { "exceptions": [<...exceptions>] }]
exceptions
选项允许你传递一个方法名称数组,你希望在旧样式中继续使用。
¥The exceptions
option allows you to pass an array of methods names you’d like to continue to use in the old style.
例如,如果你希望使用所有 Reflect 方法,除了 Function.prototype.apply
,那么你的配置将看起来像 prefer-reflect: [2, { "exceptions": ["apply"] }]
。
¥For example if you wish to use all Reflect methods, except for Function.prototype.apply
then your config would look like prefer-reflect: [2, { "exceptions": ["apply"] }]
.
如果你想使用 Reflect 方法,但继续使用 delete
关键字,那么你的配置看起来像 prefer-reflect: [2, { "exceptions": ["delete"] }]
。
¥If you want to use Reflect methods, but keep using the delete
keyword, then your config would look like prefer-reflect: [2, { "exceptions": ["delete"] }]
.
这些可以任意组合。要使所有方法异常(从而使此规则无用),请使用 prefer-reflect: [2, { "exceptions": ["apply", "call", "defineProperty", "getOwnPropertyDescriptor", "getPrototypeOf", "setPrototypeOf", "isExtensible", "getOwnPropertyNames", "preventExtensions", "delete"] }]
¥These can be combined as much as you like. To make all methods exceptions (thereby rendering this rule useless), use prefer-reflect: [2, { "exceptions": ["apply", "call", "defineProperty", "getOwnPropertyDescriptor", "getPrototypeOf", "setPrototypeOf", "isExtensible", "getOwnPropertyNames", "preventExtensions", "delete"] }]
Reflect.apply
弃用 Function.prototype.apply()
和 Function.prototype.call()
¥Deprecates Function.prototype.apply()
and Function.prototype.call()
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
myFunction.apply(undefined, args);
myFunction.apply(null, args);
obj.myMethod.apply(obj, args);
obj.myMethod.apply(other, args);
myFunction.call(undefined, arg);
myFunction.call(null, arg);
obj.myMethod.call(obj, arg);
obj.myMethod.call(other, arg);
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Reflect.apply(myFunction, undefined, args);
Reflect.apply(myFunction, null, args);
Reflect.apply(obj.myMethod, obj, args);
Reflect.apply(obj.myMethod, other, args);
Reflect.apply(myFunction, undefined, [arg]);
Reflect.apply(myFunction, null, [arg]);
Reflect.apply(obj.myMethod, obj, [arg]);
Reflect.apply(obj.myMethod, other, [arg]);
使用 { "exceptions": ["apply"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["apply"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["apply"] }]*/
// in addition to Reflect.apply(...):
myFunction.apply(undefined, args);
myFunction.apply(null, args);
obj.myMethod.apply(obj, args);
obj.myMethod.apply(other, args);
使用 { "exceptions": ["call"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["call"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["call"] }]*/
// in addition to Reflect.apply(...):
myFunction.call(undefined, arg);
myFunction.call(null, arg);
obj.myMethod.call(obj, arg);
obj.myMethod.call(other, arg);
Reflect.defineProperty
弃用 Object.defineProperty()
¥Deprecates Object.defineProperty()
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Object.defineProperty({}, 'foo', {value: 1})
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Reflect.defineProperty({}, 'foo', {value: 1})
使用 { "exceptions": ["defineProperty"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["defineProperty"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["defineProperty"] }]*/
Object.defineProperty({}, 'foo', {value: 1})
Reflect.defineProperty({}, 'foo', {value: 1})
Reflect.getOwnPropertyDescriptor
弃用 Object.getOwnPropertyDescriptor()
¥Deprecates Object.getOwnPropertyDescriptor()
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Object.getOwnPropertyDescriptor({}, 'foo')
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Reflect.getOwnPropertyDescriptor({}, 'foo')
使用 { "exceptions": ["getOwnPropertyDescriptor"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["getOwnPropertyDescriptor"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyDescriptor"] }]*/
Object.getOwnPropertyDescriptor({}, 'foo')
Reflect.getOwnPropertyDescriptor({}, 'foo')
Reflect.getPrototypeOf
弃用 Object.getPrototypeOf()
¥Deprecates Object.getPrototypeOf()
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Object.getPrototypeOf({}, 'foo')
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Reflect.getPrototypeOf({}, 'foo')
使用 { "exceptions": ["getPrototypeOf"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["getPrototypeOf"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["getPrototypeOf"] }]*/
Object.getPrototypeOf({}, 'foo')
Reflect.getPrototypeOf({}, 'foo')
Reflect.setPrototypeOf
弃用 Object.setPrototypeOf()
¥Deprecates Object.setPrototypeOf()
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Object.setPrototypeOf({}, Object.prototype)
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Reflect.setPrototypeOf({}, Object.prototype)
使用 { "exceptions": ["setPrototypeOf"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["setPrototypeOf"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["setPrototypeOf"] }]*/
Object.setPrototypeOf({}, Object.prototype)
Reflect.setPrototypeOf({}, Object.prototype)
Reflect.isExtensible
弃用 Object.isExtensible
¥Deprecates Object.isExtensible
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Object.isExtensible({})
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Reflect.isExtensible({})
使用 { "exceptions": ["isExtensible"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["isExtensible"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["isExtensible"] }]*/
Object.isExtensible({})
Reflect.isExtensible({})
Reflect.preventExtensions
弃用 Object.preventExtensions()
¥Deprecates Object.preventExtensions()
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Object.preventExtensions({})
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
Reflect.preventExtensions({})
使用 { "exceptions": ["preventExtensions"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["preventExtensions"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["preventExtensions"] }]*/
Object.preventExtensions({})
Reflect.preventExtensions({})
Reflect.deleteProperty
弃用 delete
关键字
¥Deprecates the delete
keyword
无例外地使用此规则时的错误代码示例:
¥Examples of incorrect code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
delete foo.bar; // deleting object property
无例外地使用此规则时的正确代码示例:
¥Examples of correct code for this rule when used without exceptions:
/*eslint prefer-reflect: "error"*/
delete bar; // deleting variable
Reflect.deleteProperty(foo, 'bar');
注意:有关防止删除变量的规则,请参阅 no-delete-var 代替
¥Note: For a rule preventing deletion of variables, see no-delete-var instead
使用 { "exceptions": ["delete"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "exceptions": ["delete"] }
option:
/*eslint prefer-reflect: ["error", { "exceptions": ["delete"] }]*/
delete bar
delete foo.bar
Reflect.deleteProperty(foo, 'bar');
何时不使用
¥When Not To Use It
此规则不应在 ES3/5 环境中使用。
¥This rule should not be used in ES3/5 environments.
在 ES2015 (ES6) 或更高版本中,如果你不想被告知可以使用 Reflect 的地方,你可以安全地禁用此规则。
¥In ES2015 (ES6) or later, if you don’t want to be notified about places where Reflect could be used, you can safely disable this rule.
相关规则
版本
此规则是在 ESLint v1.0.0-rc-2 中引入。