We already saw how we can add a new property in an object while declaring an object, and also even if the object is already declared we can still add new properties in an object by using dot notation (.) or square bracket notation () with new property/key name followed by assignment operator (=) with property value.
Along with, in the same way how we can change the object existing property value simply by writing the name of the object and using dot notation (.) or the bracket notation () with property/key name followed by assignment operator (=) with the new value.
As we saw the various ways for creating/ defining a new and updating an existing property to an object. Here comes another way for adding a new and modifying the existing property of an object is by using the Object.defineProperty() method.
Syntax: Object.defineProperty(obj, propName, descriptor)
- obj – It is an object name on which property is defined.
- propName – The name of the property or Symbol of the property to be defined or modified.
- descriptor – The descriptor for the property being modified or defined. It can be a data descriptor or accessor descriptor (property descriptor).
Object.defineProperty() method can be invoked with the following options:
Note: By default, the property added by using Object.defineProperty() method are immutable (Objects whose value cannot be changed) and not enumerable (an object that cannot be counted and doesn’t show up when iterating using loop are called non-enumerable). In short, the property added by this method is not enumerable, not configurable, not writable as defaults.
When creating a property using Object.defineProperty() if there is no attribute specified in the object. This method will use their default values. That means Properties created using method Object.defineProperty() have an enumerable flag set to false by default, configurable flag set to false by default, and writable flag set to false by default.
Property descriptors present in Object.defineProperty() come in two main varieties:
- Data Descriptors:
- Accessor Descriptors (property accessor):
Both data descriptors and accessor descriptors are objects. They share the below listed following optional keys.
Note: The defaults value mentioned here are in the case when properties are defined using Object.defineProperty() without any parameters:
- configurable: By default, it is false. When set to true that means the type of this property descriptor can be modified or updated later and the property can be deleted from the respective object.
- enumerable: By default, it is false. When set to true that means this property can be counted and show up when iterating using a loop (for… in) are called enumerable or during enumeration of the properties. To check whether a property is enumerable or not, the function called propertyIsEnumerable() is used. It returns true if the object property is enumerable otherwise returns false.
A data descriptor is a value of a property, which can or cannot be writable(modify).
- Required fields: value or writable or both also
- Optional fields: configurable, enumerable
A data descriptor also has the following attributes:
- writable: By default, it is false. it is read-only. When set to true that means the value of the property can be changed by using assignment operator (=) followed by the new value of a property.
A data descriptor is a value of a property, which can or cannot be writable(modify). An accessor descriptor or property accessor is a property described by a getter-setter accessor combination of functions. A descriptor must be one of these two options; either getter or setter, it cannot be both.
Getter is for reading or fetching the property value whereas setter it for setting a new or modifying the existing value of a property.
- Required fields: get or set or both
- Optional fields: configurable, enumerable
An accessor descriptor/property accessor also has the following optional:
- get: By default, it is undefined. It’s a get method that is a getter for the property, it makes the property read-only. This method returns a value of the property which means it is used for getting/fetching the property value. If no getter is provided it is undefined.
- set: By default, it is undefined. It’s a set method that is a setter for the property, it makes the property writable. This method accepts a new value as an argument getting assigned to the property which means it is used for setting a new value or modifying the existing value of a property. If no setter is provided it is undefined.
If a property descriptor does not have value, writable, get, and set keys, it is considered as a data descriptor. An exception is thrown If a property descriptor has both [value or writable] and [get or set] keys. How to make property writable, enumerable, and configured will see this in the next chapter.