admin管理员组文章数量:1336099
Using eslint with React configurations I get an error when using Object.defineProperty
. The error says:
Avoid using
Object.defineProperty
, instead useReflect.defineProperty
. (prefer-reflect)
On the eslint documentation of prefer-reflect they say it's deprecated, the reason being:
The original intent of this rule now seems misguided as we have e to understand that
Reflect
methods are not actually intended to replace theObject
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.
Then my question is: is there any advantage in using Reflect.defineProperty
instead of Object.defineProperty
?
Using eslint with React configurations I get an error when using Object.defineProperty
. The error says:
Avoid using
Object.defineProperty
, instead useReflect.defineProperty
. (prefer-reflect)
On the eslint documentation of prefer-reflect they say it's deprecated, the reason being:
The original intent of this rule now seems misguided as we have e to understand that
Reflect
methods are not actually intended to replace theObject
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.
Then my question is: is there any advantage in using Reflect.defineProperty
instead of Object.defineProperty
?
- 1 Yes, the deprecation notice explains it just fine. You should not use this rule. – Bergi Commented Mar 3, 2018 at 15:56
2 Answers
Reset to default 6There is a slight difference between Object.defineProperty
and Reflect.defineProperty
: the former returns the target object and throws an exception when the descriptor could not be applied (e.g. when the property is non-writable or non-configurable or when the object is non-extensible), while the latter does return a boolean value whether it worked.
I'd argue that this makes Object.defineProperty
a bit more useful, unless you're implementing a defineProperty
proxy trap.
Just to plement the last answer, with Object.defineProperty
you must use a try/catch
block for contolling exceptions while with Reflect.defineProperty
you just can do a boolean check with an if
statement for correct property creation.
本文标签: javascriptIs it better to use ReflectdefineProperty instead of ObjectdefinePropertyStack Overflow
版权声明:本文标题:javascript - Is it better to use Reflect.defineProperty instead of Object.defineProperty? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742402718a2468207.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论