12 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.
14 The ES6 Reflect API comes with a handful of methods which somewhat deprecate methods on old constructors:
16 * [`Reflect.apply`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.apply) effectively deprecates [`Function.prototype.apply`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-function.prototype.apply) and [`Function.prototype.call`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-function.prototype.call)
17 * [`Reflect.deleteProperty`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.deleteproperty) effectively deprecates the [`delete` keyword](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-delete-operator-runtime-semantics-evaluation)
18 * [`Reflect.getOwnPropertyDescriptor`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.getownpropertydescriptor) effectively deprecates [`Object.getOwnPropertyDescriptor`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.getownpropertydescriptor)
19 * [`Reflect.getPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.getprototypeof) effectively deprecates [`Object.getPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.getprototypeof)
20 * [`Reflect.setPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.setprototypeof) effectively deprecates [`Object.setPrototypeOf`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.setprototypeof)
21 * [`Reflect.preventExtensions`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect.preventextensions) effectively deprecates [`Object.preventExtensions`](https://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.preventextensions)
23 The prefer-reflect rule will flag usage of any older method, suggesting to instead use the newer Reflect version.
32 "prefer-reflect": [<enabled>, { "exceptions": [<...exceptions>] }]
35 The `exceptions` option allows you to pass an array of methods names you'd like to continue to use in the old style.
37 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"] }]`.
39 If you want to use Reflect methods, but keep using the `delete` keyword, then your config would look like `prefer-reflect: [2, { "exceptions": ["delete"] }]`.
41 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"] }]`
45 Deprecates `Function.prototype.apply()` and `Function.prototype.call()`
47 Examples of **incorrect** code for this rule when used without exceptions:
52 /*eslint prefer-reflect: "error"*/
54 myFunction.apply(undefined, args);
55 myFunction.apply(null, args);
56 obj.myMethod.apply(obj, args);
57 obj.myMethod.apply(other, args);
59 myFunction.call(undefined, arg);
60 myFunction.call(null, arg);
61 obj.myMethod.call(obj, arg);
62 obj.myMethod.call(other, arg);
67 Examples of **correct** code for this rule when used without exceptions:
72 /*eslint prefer-reflect: "error"*/
74 Reflect.apply(myFunction, undefined, args);
75 Reflect.apply(myFunction, null, args);
76 Reflect.apply(obj.myMethod, obj, args);
77 Reflect.apply(obj.myMethod, other, args);
78 Reflect.apply(myFunction, undefined, [arg]);
79 Reflect.apply(myFunction, null, [arg]);
80 Reflect.apply(obj.myMethod, obj, [arg]);
81 Reflect.apply(obj.myMethod, other, [arg]);
86 Examples of **correct** code for this rule with the `{ "exceptions": ["apply"] }` option:
91 /*eslint prefer-reflect: ["error", { "exceptions": ["apply"] }]*/
93 // in addition to Reflect.apply(...):
94 myFunction.apply(undefined, args);
95 myFunction.apply(null, args);
96 obj.myMethod.apply(obj, args);
97 obj.myMethod.apply(other, args);
102 Examples of **correct** code for this rule with the `{ "exceptions": ["call"] }` option:
107 /*eslint prefer-reflect: ["error", { "exceptions": ["call"] }]*/
109 // in addition to Reflect.apply(...):
110 myFunction.call(undefined, arg);
111 myFunction.call(null, arg);
112 obj.myMethod.call(obj, arg);
113 obj.myMethod.call(other, arg);
118 ### Reflect.defineProperty
120 Deprecates `Object.defineProperty()`
122 Examples of **incorrect** code for this rule when used without exceptions:
127 /*eslint prefer-reflect: "error"*/
129 Object.defineProperty({}, 'foo', {value: 1})
134 Examples of **correct** code for this rule when used without exceptions:
139 /*eslint prefer-reflect: "error"*/
141 Reflect.defineProperty({}, 'foo', {value: 1})
146 Examples of **correct** code for this rule with the `{ "exceptions": ["defineProperty"] }` option:
151 /*eslint prefer-reflect: ["error", { "exceptions": ["defineProperty"] }]*/
153 Object.defineProperty({}, 'foo', {value: 1})
154 Reflect.defineProperty({}, 'foo', {value: 1})
159 ### Reflect.getOwnPropertyDescriptor
161 Deprecates `Object.getOwnPropertyDescriptor()`
163 Examples of **incorrect** code for this rule when used without exceptions:
168 /*eslint prefer-reflect: "error"*/
170 Object.getOwnPropertyDescriptor({}, 'foo')
175 Examples of **correct** code for this rule when used without exceptions:
180 /*eslint prefer-reflect: "error"*/
182 Reflect.getOwnPropertyDescriptor({}, 'foo')
187 Examples of **correct** code for this rule with the `{ "exceptions": ["getOwnPropertyDescriptor"] }` option:
192 /*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyDescriptor"] }]*/
194 Object.getOwnPropertyDescriptor({}, 'foo')
195 Reflect.getOwnPropertyDescriptor({}, 'foo')
200 ### Reflect.getPrototypeOf
202 Deprecates `Object.getPrototypeOf()`
204 Examples of **incorrect** code for this rule when used without exceptions:
209 /*eslint prefer-reflect: "error"*/
211 Object.getPrototypeOf({}, 'foo')
216 Examples of **correct** code for this rule when used without exceptions:
221 /*eslint prefer-reflect: "error"*/
223 Reflect.getPrototypeOf({}, 'foo')
228 Examples of **correct** code for this rule with the `{ "exceptions": ["getPrototypeOf"] }` option:
233 /*eslint prefer-reflect: ["error", { "exceptions": ["getPrototypeOf"] }]*/
235 Object.getPrototypeOf({}, 'foo')
236 Reflect.getPrototypeOf({}, 'foo')
241 ### Reflect.setPrototypeOf
243 Deprecates `Object.setPrototypeOf()`
245 Examples of **incorrect** code for this rule when used without exceptions:
250 /*eslint prefer-reflect: "error"*/
252 Object.setPrototypeOf({}, Object.prototype)
257 Examples of **correct** code for this rule when used without exceptions:
262 /*eslint prefer-reflect: "error"*/
264 Reflect.setPrototypeOf({}, Object.prototype)
269 Examples of **correct** code for this rule with the `{ "exceptions": ["setPrototypeOf"] }` option:
274 /*eslint prefer-reflect: ["error", { "exceptions": ["setPrototypeOf"] }]*/
276 Object.setPrototypeOf({}, Object.prototype)
277 Reflect.setPrototypeOf({}, Object.prototype)
282 ### Reflect.isExtensible
284 Deprecates `Object.isExtensible`
286 Examples of **incorrect** code for this rule when used without exceptions:
291 /*eslint prefer-reflect: "error"*/
293 Object.isExtensible({})
298 Examples of **correct** code for this rule when used without exceptions:
303 /*eslint prefer-reflect: "error"*/
305 Reflect.isExtensible({})
310 Examples of **correct** code for this rule with the `{ "exceptions": ["isExtensible"] }` option:
315 /*eslint prefer-reflect: ["error", { "exceptions": ["isExtensible"] }]*/
317 Object.isExtensible({})
318 Reflect.isExtensible({})
323 ### Reflect.getOwnPropertyNames
325 Deprecates `Object.getOwnPropertyNames()`
327 Examples of **incorrect** code for this rule when used without exceptions:
332 /*eslint prefer-reflect: "error"*/
334 Object.getOwnPropertyNames({})
339 Examples of **correct** code for this rule when used without exceptions:
344 /*eslint prefer-reflect: "error"*/
346 Reflect.getOwnPropertyNames({})
351 Examples of **correct** code for this rule with the `{ "exceptions": ["getOwnPropertyNames"] }` option:
356 /*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyNames"] }]*/
358 Object.getOwnPropertyNames({})
359 Reflect.getOwnPropertyNames({})
364 ### Reflect.preventExtensions
366 Deprecates `Object.preventExtensions()`
368 Examples of **incorrect** code for this rule when used without exceptions:
373 /*eslint prefer-reflect: "error"*/
375 Object.preventExtensions({})
380 Examples of **correct** code for this rule when used without exceptions:
385 /*eslint prefer-reflect: "error"*/
387 Reflect.preventExtensions({})
392 Examples of **correct** code for this rule with the `{ "exceptions": ["preventExtensions"] }` option:
397 /*eslint prefer-reflect: ["error", { "exceptions": ["preventExtensions"] }]*/
399 Object.preventExtensions({})
400 Reflect.preventExtensions({})
405 ### Reflect.deleteProperty
407 Deprecates the `delete` keyword
409 Examples of **incorrect** code for this rule when used without exceptions:
414 /*eslint prefer-reflect: "error"*/
416 delete foo.bar; // deleting object property
421 Examples of **correct** code for this rule when used without exceptions:
426 /*eslint prefer-reflect: "error"*/
428 delete bar; // deleting variable
429 Reflect.deleteProperty(foo, 'bar');
434 Note: For a rule preventing deletion of variables, see [no-delete-var instead](no-delete-var)
436 Examples of **correct** code for this rule with the `{ "exceptions": ["delete"] }` option:
441 /*eslint prefer-reflect: ["error", { "exceptions": ["delete"] }]*/
445 Reflect.deleteProperty(foo, 'bar');
450 ## When Not To Use It
452 This rule should not be used in ES3/5 environments.
454 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.