A data descriptor defines an object property with a stored value and controls such as writability, enumerability, and configurability. Use Object.defineProperty when direct assignment does not give you the property behavior you need. section 9.1.2
When defining a property with a data descriptor:
- Provide a value; writable, enumerable, and configurable default to false. section 9.1.2
- A non-writable property rejects assignment; in strict mode, that assignment throws an error. section 9.1.2
- A non-configurable, non-writable property is effectively permanent. If writable is true but configurable is false, its value can change, but other aspects of its definition cannot. section 9.1.2
const obj = {};
Object.defineProperty(obj, "surfNinjaCount", {
value: 3,
});
console.assert(
obj.surfNinjaCount === 3,
"There are 3 surf ninjas.",
);
A minimal Object.defineProperty call supplies a value for the new property. section 9.1.2
