prefer-reflect

此规则在 ESLint v3.9.0 中已弃用,不会被替换。这条规则的初衷现在似乎被误导了,因为我们已经了解到 Reflect 方法实际上并不是要替换规则建议的 Object 对应物,而是作为低级原语存在,与代理一起使用以复制默认值各种先前存在的功能的行为。

请注意:此规则包含不正确的行为 - 它会建议您使用 Reflect.getOwnPropertyNames 而不是 Object.getOwnPropertyNames,但前一个在 规范 中不存在。我们建议将 exceptions 选项与 "getOwnPropertyNames" 一起使用,以避免这种错误建议。

ES6 Reflect API 附带了一些方法,它们在一定程度上弃用了旧构造函数的方法:

prefer-reflect 规则将标记任何旧方法的使用,建议改用较新的 Reflect 版本。

规则详情

选项

异常

"prefer-reflect": [<enabled>, { "exceptions": [<...exceptions>] }]

hapZNUq1LO1zBA1H0li+FX8cL0xITZdrjz8MF243klzFYd+aSloDDT4432XYyU6Ynu5GgqEaDyyp5PMyZv76RVAJHS9uQAGDB/5kU5ho4sS+JhLyO7bxyBj9VS9PSr9JLP05sq3hjtCTLBhMYc+wZg==

3UusS6B40zticcq+dVV+wxE6cmgXxx0lFUF/lA7tI+kTTy9so9fyWvjVEZNKXm3oJOMz8kodzExw3Y33e9tLgdYYGXawKh+CapCF6z6QWHXzt2KEQ1H/GoqZycyuRJMrwoBVMxjrct7r2zAB2mhn8jtjnSBDyHgi9i7N31r/qqrKMhSzqXBdt50vZUoralHPjGIlCi1ytQVTOd2/YvLCzmQwbo1fHQ858x27Fbwo8NJXHGRegbL3Fub2CWg880Z+s8lctwZcng9EtNzUhPmuHA==

DYaVkjYMlVpktCI98j2mipVIaomlSktniKtj4wNod+3cOfRLLW+ugEsZ17CoL5/45Fx+In3yNvv2wOR9lBtoilXtdOPJtK4naFsAvWHf3vK5/2FLZTylPVQsnqVhbYDYrrzr094k0KDwhKx7qEtiqJt72Q6ho3InryKD2kfYyjxeQZodX83oD+Qo/c6ft0IXF4gXrJp6YfijYVBt2BL7oCS1crhjIxaMjAwwfomapPLc+G0sVm27tt/9DNq3YTH9

aAdRcVoOSzrfWixyL8h8EHe++wXhevOiYUgLYvhaweF85FzlXJupQzT5azyeSJqG6WPBiNs34myQWVBFeLtojQf7YuvWpij3AWYaDA0M8UGRWE4Uu4JEoHz5y8PnLdKwyyBvdmwpQQ9csj8rfiKxiL+EreSDHpYhRvRblyrXsdlmh6flZTkGUQ/phs2YWYCq39FXPANqkHd89wVbIaRYGN3XLAC7GBvdnn4Yr2LzVXDyY3TzfkYXmmcL9Agvv55QJJPNP4pLdw+tH2ZP0dGBGPvgcfwJoWq1Ma1QytUvjgxesK/Cstm4FepUeFIesbZ473rQ/Bke2uYIcq4Mwlr01BIq0IoP7RT/aFogd8aiS8e0TTOsA/B9hG4CM62TtbLLfDSabtnjbfXMFfSksKYTzNfuz5JiIQ0UVDcstnCZLqo=

Reflect.apply

rSZP4gmDdB32YbgZyumynJVM5rbsJFluhQR8/0r48fT1ZZ/1rqHAkO7xJDHOq48EM8xyyzxmv6e2YzaIwLnWdjKddK+ez7ULoqyT4y9ZvmOeec2qtdnkvc/tn9UgT6kJ

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*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);

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*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]);

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx4uQJSDHVcNwMUX91qZfAQ+w0+9rxZhlSkq/5BWYCZyl5FoWA+ynSCW4/i3lyt7hz4yNiIWyr9xC7YQqAsAmUs+

/*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);

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx6e+Xmbf07aBQ0cFwBMz5rbyJeyyH3PByhv6ClLLK7+x6u9pexz2r1mYALPk9iDe6k5kzk+Rs/7F8t00VjfCMsR

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

ez8EW4lKoLT5/qu1rAuppKUrPwZnBoGtSCnezvroijZ6uV3sBITZ3JpJR3OJEUXN

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*eslint prefer-reflect: "error"*/

Object.defineProperty({}, 'foo', {value: 1})

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*eslint prefer-reflect: "error"*/

Reflect.defineProperty({}, 'foo', {value: 1})

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx6jIc4/g5vJd4EX2k5WCl7hqXaNiDPDs1yRx0tcvXtyIO9yIfUZbvTgeYCt8b1YW8GsJXLlHxXBgdCWsrFLOOb169e0XsXxyAE+PBd/AlGhOA==

/*eslint prefer-reflect: ["error", { "exceptions": ["defineProperty"] }]*/

Object.defineProperty({}, 'foo', {value: 1})
Reflect.defineProperty({}, 'foo', {value: 1})

Reflect.getOwnPropertyDescriptor

ez8EW4lKoLT5/qu1rAuppKONHlss20h5B7J5X0M3hE6wfbxChbbsorgZX8BpvtL4cQfeBYu3M4bEad2UC6mj0A==

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*eslint prefer-reflect: "error"*/

Object.getOwnPropertyDescriptor({}, 'foo')

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*eslint prefer-reflect: "error"*/

Reflect.getOwnPropertyDescriptor({}, 'foo')

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx7lF/AQjxyQ42ChJ8ifKt211V2G8tcnxVSPRBiXFgtzJ+/NfDrkiFL+7eSpOc2APex1Y1stzzbpP5uAKk/wpcq/KbY9GXGVPnvYgAXQZSMJmw==

/*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyDescriptor"] }]*/

Object.getOwnPropertyDescriptor({}, 'foo')
Reflect.getOwnPropertyDescriptor({}, 'foo')

Reflect.getPrototypeOf

ez8EW4lKoLT5/qu1rAuppCTlCExulokNan4I5TOXo1OiGJ658dyQH9rBxUTIAUsr

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*eslint prefer-reflect: "error"*/

Object.getPrototypeOf({}, 'foo')

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*eslint prefer-reflect: "error"*/

Reflect.getPrototypeOf({}, 'foo')

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx4b1K4+zkTZhlNbOMszGKgmPO0jvLcB9NEGXldOxnQeACDT0uPvn/4qvInmrGY3ARFYegd9qMohXRVLDKFMn4kHDPUHYffqk3ghbwzRRSFPCQ==

/*eslint prefer-reflect: ["error", { "exceptions": ["getPrototypeOf"] }]*/

Object.getPrototypeOf({}, 'foo')
Reflect.getPrototypeOf({}, 'foo')

Reflect.setPrototypeOf

ez8EW4lKoLT5/qu1rAuppHoq0Jj+Wg3Z1sxeqU/WfhIrHHGcwfMSSf1N4WIHe8UW

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*eslint prefer-reflect: "error"*/

Object.setPrototypeOf({}, Object.prototype)

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*eslint prefer-reflect: "error"*/

Reflect.setPrototypeOf({}, Object.prototype)

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx7+uUodauZIIun9OoXrJWFSFygb1TNyWhZXF7YSob0hFlsm8XwLxZLJNfvEkaQMLiJ8UBAo5GgqQEnyMIowCKPXQpatqlxnoBV76TOX2fMkOA==

/*eslint prefer-reflect: ["error", { "exceptions": ["setPrototypeOf"] }]*/

Object.setPrototypeOf({}, Object.prototype)
Reflect.setPrototypeOf({}, Object.prototype)

Reflect.isExtensible

ez8EW4lKoLT5/qu1rAuppNSPmAn5Y34OvFvbpqYHVDfWCpi8hFnRdC7XtJYY28ll

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*eslint prefer-reflect: "error"*/

Object.isExtensible({})

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*eslint prefer-reflect: "error"*/

Reflect.isExtensible({})

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx5sXIfUWdC2pF4neeKfODFrhs7kE+/yUSiO86w30B76nQnMmr3ggoXrYIeZp7/Msz7tKzoTr8j9ssZbdFYxrfYl

/*eslint prefer-reflect: ["error", { "exceptions": ["isExtensible"] }]*/

Object.isExtensible({})
Reflect.isExtensible({})

Reflect.preventExtensions

ez8EW4lKoLT5/qu1rAuppGOyyKPrpz/NpV9iz/emIZwsoBCnK8DjbgrWv9rxJnP5

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*eslint prefer-reflect: "error"*/

Object.preventExtensions({})

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*eslint prefer-reflect: "error"*/

Reflect.preventExtensions({})

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx4KFiUtfZF8S+LXJtb2evmit3MPb1A3VxmX+2ORo/k2OeaPx7FZboxtyYYRW6Yf4skosQv85x82wiJp2IZ+7HWqmD2cfpfefFNM3wVOxybVfg==

/*eslint prefer-reflect: ["error", { "exceptions": ["preventExtensions"] }]*/

Object.preventExtensions({})
Reflect.preventExtensions({})

Reflect.deleteProperty

uZDj69a0Ly09I5OKhb3/r2qJWPgyBkNJROphRyF83/GGjOuZ5zMnZx7GLPCqp7LR

9mDhJXwFWRszWkw2CPgP2FOdg0L5ndrbpc/G/Tuoj/Gg/GNGH67wexKyABW6rwrblsf2WpopAFAa7vwYB50mUQ==

/*eslint prefer-reflect: "error"*/

delete foo.bar; // deleting object property

9mDhJXwFWRszWkw2CPgP2Fy/20DJg9BH8ox+++oUei8eQNESE1cGf9PZmq8jF8JNpjm2hDigsHJ7fv/nlPuJow==

/*eslint prefer-reflect: "error"*/

delete bar; // deleting variable
Reflect.deleteProperty(foo, 'bar');

MtfsM+Xfs6TesE/WE8IjJbhVQGsv44mnpCBHBvnVUdZMyuNF7FHLwYDIpF+HwG041g/XyedA4Ri4Gd6HAcbeDCdHydjgXq9SmyFDIYsv9XjDK0d08AAws34BRBeLFp2VevvnLByU6IddpRv24VM2bkSzPkaMdJXQAlx0NNXEcn8=

dg8a89Fcmdiuqkj/gGSI6qz53KHvcGwyfp+DTHjXtx4Qo9lRgdFVEwUKsEwgcwt/mRVYSTgdfZfNHEVr8PSVATfVjitZKNbmzc63aqYtWdPq2Wyjn7nrgGtK2hiay8Dk

/*eslint prefer-reflect: ["error", { "exceptions": ["delete"] }]*/

delete bar
delete foo.bar
Reflect.deleteProperty(foo, 'bar');

何时不使用

6xTSq91asfSAetpbRaQCMI9Y6IvfOvRH+qAEggadXj2Ayf9sYwAGrECWf7W4nE4T

YYbbipkzWNo8gsOM6q/uxB8OGxpFRa18AUon7NKdfSAryp9ejGIXNgu2MN1N30KiqFs/hq/WWzMNcTdYErjTnj3Bg70oElkWhRu/EBE2D/E/hxZSJWTRWCVsaq9n79jxm2JSem6xQpmT5mx4s3d2i7Ev0gMVEofKGLwZknbjS5DFy7FjBCMyp3RgofdtPXqd