Lock property description
suggest changeA property’s descriptor can be locked so no changes can be made to it. It will still be possible to use the property normally, assigning and retrieving the value from it, but any attempt to redefine it will throw an exception.
The configurable property of the property descriptor is used to disallow any further changes on the descriptor.
const obj = {};
// Define 'foo' as read only and lock it
Object.defineProperty(obj, "foo", {
value: "original value",
writable: false,
configurable: false
});
Object.defineProperty(obj, "foo", {writable: true});
This error will be thrown:
TypeError: Cannot redefine property: foo
The property stays read-only. In strict mode the assignment throws TypeError. In non-strict code it is ignored. Log obj.foo, not the bare name foo.
obj.foo = "new value";
console.log(obj.foo); // "original value"
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents