Redefine an existing property to an object

const o = { key: '1' };
Object.defineProperty(o, 'key', { value: '2' });
// configurable of `o.key` is true

Object.defineProperty(o, 'newKey', { value: '2' });
// configurable of `o.newKey` is false

I have read the spec of DefineProperty, the result of above code is correct, but It's still confusing for me.
Why not make the configurable of property key to be the default value false? As the document at MDN:

1 Like

The ‘key’ property was already defined, so ‘defineProperty’ is modifying the existing definition. The default values are only applied when creating a new property.

This did suprise me the first time I found this out and potentially ‘defineProperty’ was not the best name for that api.

It might be worth suggesting an update on the MDN page to ensure this behaviour is clearer.

1 Like

Is it possible to revise the DefineProperty to make the definition clearer?

If you're talking about MDN, then you can make edit suggestions through github: Edit, Report.