Javascript - Class Data Properties

Introduction

There are two types of properties: data properties and accessor properties.

Data properties contain data value. Values are read from and written to data properties.

Data properties have four attributes describing their behavior:

Item Description
[[Configurable]] if the property may be redefined by removing the property via delete, changing the property's attributes, or changing the property into an accessor property. By default, this is true for all properties defined directly on an object.
[[Enumerable]]if the property will be returned in a for-in loop. By default, this is true for all properties defined directly on an object.
[[Writable]] if the property's value can be changed. By default, this is true for all properties defined directly on an object.
[[Value]] Contains the actual data value for the property. This is the location from which the property's value is read and the location to which new values are saved. The default value for this attribute is undefined.

When a property is explicitly added to an object, [[Configurable]], [[Enumerable]], and [[Writable]] are all set to true while the [[Value]] attribute is set to the assigned value. For example:

var person = {
    name: "First"
};

Here, the property called name is created and a value of "First" is assigned.

[[Value]] is set to "First", and any changes to that value are stored in [[Value]].

To change the default property attributes, use the ECMAScript 5 Object method defineProperty().

This method accepts three arguments:

  • the object,
  • the name of the property, and
  • a descriptor object.

The properties on the descriptor object match the attribute names: configurable, enumerable, writable, and value.

You can set one or all of these values to change the corresponding attribute values.

Demo

var person = {};
Object.defineProperty(person, "name", {
   writable: false,/*w  w w.  j a  v a  2  s  . c  o m*/
   value: "First"
});
console.log(person.name);    //"First"
person.name = "Tom";
console.log(person.name);    //"First"

Result

This example creates a property called name with a read-only value of "First".

The value of this property can't be changed, and any attempts to assign a new value are ignored in nonstrict mode.

In strict mode, an error is thrown when an attempt is made to change the value of a read-only property.

You can create a nonconfigurable property. For example:

Demo

var person = {};
Object.defineProperty(person, "name", {
    configurable: false,/*from   ww w  .jav a  2  s  .c  om*/
    value: "First"
 });

console.log(person.name);    //"First"
delete person.name;
console.log(person.name);    //"First"

Result

Here, setting configurable to false means that the property cannot be removed from the object.

Calling delete on the property has no effect in nonstrict mode and throws an error in strict mode.

Once a property has been defined as nonconfigurable, it cannot become configurable again.

Any attempt to call Object.defineProperty() and change any attribute other than writable causes an error:

var person = {};
Object.defineProperty(person, "name", {
   configurable: false,
     value: "First"
});

//throws an error
Object.defineProperty(person, "name", {
    configurable: true,
    value: "First"
});

When using Object.defineProperty(), configurable, enumerable, and writable are default to false unless otherwise specified.

In most cases, you don't need the powerful options provided by Object.defineProperty().