var person = {};
Object.defineProperty(person,"name",{
writable: true,
configurable: false,
value: "Sahand"
});
alert(person.name); // "Sahand"
person.name = "Mahmoud";
alert(person.name); // "Mahmoud"
Object.defineProperty(person, "name", {
writable: false
});
person.name = "Sandra"; // "Mahmoud"
alert(person.name);
Object.defineProperty(person, "name", { // Error
writable:true
});
person.name = "Sahand";
In this code, defineProperty()
works fine the first two times it is called, but throws an error the third time. This is confusing to me, since I thought if the object had configurable:false
in the first defineProperty()
call, any successive defineProperty()
calls would throw an error. Instead, the first one does not, but the other one does, after setting writable:false
. What is the rule here?