Javascript Object Oriented Design - Javascript Data Property Attributes

Data properties possess two additional attributes.

[[Value]] holds the property value.

This attribute is filled in automatically when you create a property on an object.

All property values are stored in [[Value]], even if the value is a function.

[[Writable]] property is a Boolean value indicating whether the property can be written to.

By default, all properties are writable unless you specify otherwise.

With these two additional attributes, you can fully define a data property using Object.defineProperty().


Consider this code:

var book1 = { 
    name : "Javascript" 

We have seen the code above and it adds the name property to book1 and sets its value.

We can achieve the same result using the following code:

var book1 = {}; 

Object.defineProperty(book1, "name", { 
    value : "Javascript", 
    enumerable : true, 
    configurable : true, 
    writable : true 

When defining a new property with Object.defineProperty(), it's important to specify all of the attributes because Boolean attributes automatically default to false otherwise.


The following code creates a name property that is nonenumerable, nonconfigurable, and nonwritable because it doesn't explicitly make any of those attributes true in the call to Object.defineProperty().

var book1 = {}; 
/*from w w  w . ja v a 2 s .  c om*/
Object.defineProperty(book1, "name", { 
    value : "Javascript" 

console.log("name" in book1);                      // true 
console.log(book1.propertyIsEnumerable("name"));   // false 

console.log("name" in book1);                      // true = "CSS"; 
console.log(;                           // "Javascript" 

The code above generates the following result.

Nonwritable properties throw an error in strict mode when you try to change the value.

In nonstrict mode, the operation silently fails.