admin管理员组文章数量:1331849
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 ofAgent.prototype
(that was never defined anywhere) immutable, but then it goes on to configure the.id
property of theagent
instance. Would you please share what yourAgent
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
1 Answer
Reset to default 9The 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.
版权声明:本文标题:javascript - Make existing non-writable and non-configurable property writable and configurable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265222a2443261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论