Javascript Object Oriented Design - Javascript Property Attributes








Retrieving Property Attributes

To get property attributes, use Object.getOwnPropertyDescriptor().

This method works only on own properties.

This method accepts two arguments: the object to work on and the property name to retrieve.

If the property exists, you should receive a descriptor object with four properties: configurable, enumerable, and the two others appropriate for the type of property.

For example, the following code creates a property and checks its attributes:


var book1 = { 
    name : "Javascript" 
}; // w  ww. j  a va2 s  . c  o m

var descriptor = Object.getOwnPropertyDescriptor(book1, "name"); 

console.log(descriptor.enumerable);     // true 
console.log(descriptor.configurable);   // true 
console.log(descriptor.writable);       // true 
console.log(descriptor.value);          // "Javascript" 

The code above generates the following result.





Preventing object modification

Objects have internal attributes that govern their behavior.

[[Extensible]] attribute is a Boolean value indicating if the object itself can be modified.

All objects we create are extensible by default, meaning new properties can be added to the object at any time.

By setting [[Extensible]] to false, we can prevent new properties from being added to an object.

There are three different ways to accomplish this.

Preventing Extensions

One way to create a nonextensible object is to use Object.preventExtensions() method.

This method accepts the object as its single argument.

After using this method on an object, we can never add any new properties to it again.

We can check the value of [[Extensible]] by using Object.isExtensible().

The following code shows examples of both methods at work.


var book1 = { 
    name : "Javascript" 
}; /* w ww  .ja v  a 2s.  c  om*/

console.log(Object.isExtensible(book1));      // true 

Object.preventExtensions(book1); 
console.log(Object.isExtensible(book1));      // false 

book1.writeLine = function() { 
     console.log(this.name); 
}; 

console.log("writeLine" in book1);              // false 

The code above generates the following result.

Attempting to add a property to a nonextensible object will throw an error in strict mode.

In nonstrict mode, the operation fails silently.





Sealing Objects

The second way to create a nonextensible object is to seal the object.

A sealed object is nonextensible, and all of its properties are nonconfigurable.

We can not add new properties to the object, and we can't remove properties or change their type either.

If an object is sealed, you can only read from and write to its properties.

We can use the Object.seal() method on an object to seal it.

After calling the Object.seal() method, the [[Extensible]] attribute is set to false, and all properties have their [[Configurable]] attribute set to false.

We can check to see whether an object is sealed using Object.isSealed() as follows:


var book1 = { 
    name : "Javascript" 
}; // w w  w.  j  a v a 2  s. c o  m

console.log(Object.isExtensible(book1));      // true 
console.log(Object.isSealed(book1));          // false 

Object.seal(book1); 
console.log(Object.isExtensible(book1));      // false 
console.log(Object.isSealed(book1));          // true 

book1.writeLine = function() { 
    console.log(this.name); 
}; 

console.log("writeLine" in book1);              // false 

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

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

var descriptor = Object.getOwnPropertyDescriptor(book1, "name"); 
console.log(descriptor.configurable);           // false 

The code above generates the following result.

In strict mode we can get an error when someone tries to use the object incorrectly with sealed objects.

Freezing Objects

The last way to create a nonextensible object is to freeze it.

If an object is frozen, you can't add or remove properties, you can't change properties' types, and you can't write to any data properties.

A frozen object is a sealed object where data properties are also read-only.

Frozen objects can't become unfrozen after being freezed.

We can freeze an object by using Object.freeze() and determine if an object is frozen by using Object.isFrozen().

For example:


var book1 = { 
    name : "Javascript" 
}; /* w  w w  .ja  v a  2s  .co  m*/

console.log(Object.isExtensible(book1));      // true 
console.log(Object.isSealed(book1));          // false 
console.log(Object.isFrozen(book1));          // false 

Object.freeze(book1); 
console.log(Object.isExtensible(book1));      // false 
console.log(Object.isSealed(book1));          // true 
console.log(Object.isFrozen(book1));          // true 

book1.writeLine = function() { 
    console.log(this.name); 
}; 

console.log("writeLine" in book1);              // false 

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

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

var descriptor = Object.getOwnPropertyDescriptor(book1, "name"); 
console.log(descriptor.configurable);           // false 
console.log(descriptor.writable);               // false 

The code above generates the following result.