some unexpected js behavior i wanted to note
See original GitHub issueReflect.defineProperty
and Object.defineProperty
seem to differ in behavior slightly
In one case I got an error with Object.defineProperty
but not Refelect.defineProperty
TypeError: Cannot redefine property: length
Another was that, as far as i know, the only way to transition a non-configurable but writable property descriptor to writable false, is via a Object.freeze
{ value: x, configurable: false, writable: true }
to
{ value: x, configurable: false, writable: false }
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
What is the oddest JavaScript behavior? - DEV Community
JavaScript is notorious for its inconsistencies in a lot of areas. It's also powerful and popular and has a lot going for it....
Read more >The 10 Most Common JavaScript Issues Developers Face
The common errors that developers make while coding in JavaScript include mistaken thinking about how the "this" keyword works, incorrect assumptions about ...
Read more >Unexpected behavior with Object.assign - Stack Overflow
I use Object.assign() in several projects for a while and so far didn't have any unexpected experiences ...
Read more >Unexpected promise behavior · Issue #13678 · nodejs/node
TypeError: Promise resolve or reject function is not callable results. Unlike every other extending in JavaScript, somehow the inner class ...
Read more >What went wrong? Troubleshooting JavaScript - MDN Web Docs
These errors generally mean that you've left off a string value's opening or closing quote mark. In the first error above, string would...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The definition of
Reflect.defineProperty
is at https://tc39.es/ecma262/#sec-reflect.defineproperty as a trivial wrapper around the internal [[DefineOwnProperty]] method, which indicates success by returning true or false.The definition of
Object.defineOwnProperty
is at https://tc39.es/ecma262/#sec-object.defineproperty as a trivial wrapper around the internal [[DefinePropertyOrThrow]] internal function at https://tc39.es/ecma262/#sec-definepropertyorthrow which is a trivial wrapper around the internal [[DefineOwnProperty]] method.From these, the only difference should be how they indicate normal failure.
Reflect.defineProperty
indicates normal failure by returning false.Object.defineOwnProperty
turns that false case into a thrownTypeError
. They should have exactly the same success behavior.(Note that there’s a PR around somewhere — that I am surprised hasn’t been merged yet — to allow
Object.defineProperty
, in one particular awful break-the-web compat case, to indicate failure by returning false rather than throwing aTypeError
. This applies only to the browser global object, i.e., the so-called “WindowProxy object” (which is not a proxy). Attn @ljharb where is this and why hasn’t it been merged?)At https://web.archive.org/web/20160603161655/http://wiki.ecmascript.org/doku.php?id=es3.1:attribute_states is my best explanation of the possible state transitions of an EcmaScript property. It explains the diagram:
In the case where
Object.defineProperty
gave you the TypeError and presumably changed nothing, didReflect.defineProperty
instead return false and also equally change nothing?Object.freeze
should only be equivalent to a call toObject.preventExtensions
and multiple calls toObject.defineProperty
. BothObject.defineProperty
andReflect.defineProperty
should be just as effective asObject.freeze
at causing a writable, non-configurable property to become non-writable, non-configurable.If any of these don’t seem right, could you post some compact test cases?
Closing since I don’t see any remaining actionable work. Please reopen and clarify otherwise.