0
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?

Sahand
  • 7,980
  • 23
  • 69
  • 137
  • Possible duplicate of [Why can I set \[enumerability and\] writability of unconfigurable property descriptors?](https://stackoverflow.com/q/9829817/1048572) – Bergi Sep 24 '17 at 18:18

1 Answers1

0

From MDN's Object.defineProperty docs:

The configurable attribute controls at the same time whether the property can be deleted from the object and whether its attributes (other than writable to false) can be changed.

So any invalid configuration of a non-configurable object will throw. But changing it to writable: false is not invalid, even when non-configurable.

llama
  • 2,535
  • 12
  • 11