Search within TutorialsPark

Javascript Objects: Properties


The adjectives that collectively describe an object are called as properties.


Javascript Object : Type of Properties

In Javascript latest specification are defined by ECMA-262 fifth edition, all latest browsers implement these specifications in their Javascript engines.

It describes characteristics of properties by the use of internal only attributes, these attributes are not directly accessible in Javascript.

To show that an attribute in an internal only, the attribute name should be surrounded with two pair of square brackets. For eg: [[Writable]]

There are two types of properties Data Properties and Accessor Properties


Javascript Objects : Data Properties

The Javascript Data Properties contain a single location to a data value. All values are read from and written to this set location only

The behaviour of Data properties are defined by four attributes explained below

Attributes Description
[[Configurable]]

It specifies that the property may be redefined by either removing the property(via delete), changing the property's attribute or converting to an accessor property.

The default value is true for all properties which are defined directly on the object.

[[Enumerable]]

It specifies if the property will be returned in for a for-in loop.

The default value is true for all properties which are defined directly on the object.

[[Writable]]

It holds the actual data value of the property, this is the location from which the property's value are read and new values saved.

The default value is undefined for all properties which are defined directly on the object.

[[Value]]

It holds the actual data value of the property, this is the location from which the property's value are read and new values saved.

The default value is undefined for all properties which are defined directly on the object.

When a property is explicitly defined for an object, all but [[Value]] attribute is set to true , while the attribute [[Value]] is set to the assigned value.

In order to alter any of the default property attributes, the method Object.defineProperty() is used.

This method accepts three arguments

1. The object to which the property should be added or modified

2. property name

3. descriptor object

Example: Javascript Object Data Properties

Give it a TRY! » Note: The object literals are a more preferred pattern over this method of creating objects


In the below demo, the attribute configurable is set to true as result of which the property cannot be removed from the object.

Once the attribute is set to nonconfigurable, it cannot be set to configurable again, calling Object.defineProperty() and trying to change any attribute other than writable will throw an error.

Example 2: Javascript Objects Data Properties

Give it a TRY! » Note: Using delete on the property has no effect.


Javascript Objects : Accessor Properties

The Accessor properties do not hold any data values.Instead, they hold a combination of getter and setter functions

Whenever an accessor property is read from, the getter function is called, the function is supposed to return a valid value.

When an accessor property is written to, the setter function is called with the new value, the is supposed to decide how to react to the new data.

The following are the attributes of the Accessor Properties:

The accessor properties cannot be defined explicitly, it can be defined only by using Object.defineProperty()

Attributes Description
[[Configurable]]

It specifies that the property may be redefined by either removing the property(via delete), changing the property's attribute or converting to a data property.

The default value is true for all properties which are defined directly on the object.

[[Enumerable]]

It specifies if the property will be returned in for a for-in loop.

The default value is true for all properties which are defined directly on the object.

[[Get]]

.The function to call when the property is read from.

The default value is undefined for all properties which are defined directly on the object.

[[Set]]

The function to call when the property is written to.

The default value is undefined for all properties which are defined directly on the object.

Example : Javascript Objects Accessor Properties

Give it a TRY! » Note: The Object() constructor with keyword new is called to create an instance of the object called car.


Javascript Object: Defining Multiple Properties

You can define multiple properties using the method Object.defineProperties(), using this method you can define multiple properties using descriptors at once.

There are two arguments to this method:

1. The object on which the properties are to be added or modified

2. An object whose property name corresponds to the propeties name which is to be added or modified

Example: Javascript Objects Multiple Properties

Give it a TRY! » Note:The code defines two properties and an accessor property.


Javascript Object: Reading Property Attributes

The property descriptor for a given property can be described using method Object.getOwnPropertyDescriptor().

This method accepts two arguments:

1. The object on which the property resides

2. The name of the property whose descriptor should be retrieved

Example: Javascript Objects Reading Property Attributes

Give it a TRY! » Note: The return value is an object.