最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make existing non-writable and non-configurable property writable and configurable - Stack Overflow

programmeradmin5浏览0评论

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 make the id property immutable:

Object.defineProperty(Agent.prototype, 'id', {
    configurable: false,
    writable: false
})

But at some point I will want to mark the object for deletion. And because we can't actually delete this, I'm going to cripple the object by removing the id property instead. So I go to make the property writable again:

Object.defineProperty(agent, 'id', {
    configurable: true,
    writable: true
})
delete agent.id

But of course I get:

TypeError: Cannot redefine property: id 

Because id already exists.

How can I make an existing non-writable property writable?

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 make the id property immutable:

Object.defineProperty(Agent.prototype, 'id', {
    configurable: false,
    writable: false
})

But at some point I will want to mark the object for deletion. And because we can't actually delete this, I'm going to cripple the object by removing the id property instead. So I go to make the property writable again:

Object.defineProperty(agent, 'id', {
    configurable: true,
    writable: true
})
delete agent.id

But of course I get:

TypeError: Cannot redefine property: id 

Because id already exists.

How can I make an existing non-writable property writable?

Share edited Jan 6, 2016 at 21:26 brandonscript asked Jan 6, 2016 at 21:18 brandonscriptbrandonscript 73.1k35 gold badges175 silver badges237 bronze badges 3
  • read webreflection.blogspot.in/2011/10/…. It might help – Ravi Rupeliya Commented Jan 6, 2016 at 21:33
  • Wat??? First of all, please understand that delete does not mark an object for deletion. And yor example is pretty incoherent: it makes the .id property of Agent.prototype (that was never defined anywhere) immutable, but then it goes on to configure the .id property of the agent instance. Would you please share what your Agent constructor does? – Bergi Commented Feb 14, 2018 at 20:41
  • This is not a variable per se, this is a property of a larger class. If you really want to know, this is part of an SDK that deals with read-only or write-once properties of an API response. The intent was to allow properties to reflect the behavior of the API, rather than having misrepresented properties pollute the SDK instance. – brandonscript Commented Feb 14, 2018 at 23:29
Add a ment  | 

1 Answer 1

Reset to default 9

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)

  1. 暂无评论