Make existing non-writable and non-configurable property writable and configurable

前端 未结 1 1989
野趣味
野趣味 2021-01-19 05:51

Say I\'ve got an object:

var agent = new Agent({name: \'James\', type: \'secret\', id: 007})

When I built the Agent class, I decided to mak

相关标签:
1条回答
  • 2021-01-19 06:16

    The Mozilla documentation says

    When the property already exists, Object.defineProperty() attempts to modify the property according to the values in the descriptor and the object's current configuration. If the old descriptor had its configurable attribute set to false (the property is said to be “non-configurable”), then no attribute besides writable can be changed. If a property is non-configurable, its writable attribute can only be changed to false.

    In other words, you must set configurable to true in the first property definition if you want to modify the property definition (to be writable) later.

    Note that you can go the other way (make writable property non-writable) when configurable is false, but that is the opposite of what you're doing here.

    0 讨论(0)
提交回复
热议问题