All properties have a name and a value, with the value being one of the property’s attributes.
Other attributes include: enumerable, configurable, and writable.
These attributes determine how the property can be accessed (e.g., is it readable? Is it writable?).
In JavaScript, all attributes can be read, but only the value attribute can be modified — and only if the property is writable.
(ECMAScript 5 provides methods for both retrieving and setting all property attributes.)
The following property metadata can be modified:
writable : true // Property value can be changed enumerable : true // Property can be enumerated configurable : true // Property can be reconfigured |
writable : false // Property value can not be changed enumerable : false // Property can be not enumerated configurable : false // Property can be not reconfigured |
Getters and setters can also be modified.
// Defining a getter get: function() { return language } // Defining a setter set: function(value) { language = value } |
This example makes the language property read-only:
Object.defineProperty(person, “language”, {writable:false}); |
This example makes the language
property non-enumerable:
Object.defineProperty(person, “language”, {enumerable:false}); |