question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

some unexpected js behavior i wanted to note

See original GitHub issue

Reflect.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:closed
  • Created 4 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
erightscommented, Jan 5, 2020

Reflect.defineProperty and Object.defineProperty seem to differ in behavior slightly

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 thrown TypeError. 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 a TypeError. 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: property-statechart

In the case where Object.defineProperty gave you the TypeError and presumably changed nothing, did Reflect.defineProperty instead return false and also equally change nothing?

Object.freeze should only be equivalent to a call to Object.preventExtensions and multiple calls to Object.defineProperty. Both Object.defineProperty and Reflect.defineProperty should be just as effective as Object.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?

0reactions
kriskowalcommented, Aug 13, 2020

Closing since I don’t see any remaining actionable work. Please reopen and clarify otherwise.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found