Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

nonconfigurable doesn't usually work as one might expect due to bug in JS spec #58

Closed
jayphelps opened this issue Mar 22, 2016 · 0 comments

Comments

@jayphelps
Copy link
Owner

Guessing this is due to an apparent oversight in the spec, since every browser I tried had this behavior.

In raw JS, to show the issue: REPL

function Foo() {}

Object.defineProperty(Foo.prototype, 'bar', {
  value: 1,
  configurable: false,
  writable: true
});

Object.defineProperty(Foo.prototype, 'bar', {
  value: 2
});
// You might expect this to error but it doesn't!

Instead, it only does what you would expect if the descriptor is writable: false or is a getter/setter combo (which doesn't have a writable prop in the descriptor) REPL

function Foo() {}

Object.defineProperty(Foo.prototype, 'bar', {
  value: 1,
  configurable: false,
  writable: false
});

Object.defineProperty(Foo.prototype, 'bar', {
  value: 2
});
// TypeError: Cannot redefine property: bar

However this also obviously makes the property readonly, which isn't always desired and not the same thing as being nonconfigurable.

var foo = new Foo();
foo.bar = 3;
// TypeError: Cannot assign to read only property 'bar' of object '#<Foo>'

Most examples I've seen demoing configurable: false omit the writable property all-together, which means it defaults to writable: false so I imagine most haven't noticed this quirk.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant