Learn Javascript Object.defineProperty() method for Beginners

Javascript Object.defineProperty() method allows to add and update new property to object. We can also assign the property to the object using the assignment operator line person.age=21 this will create property age in person object.
But when we define property using the Object.defineProperty() method we can pass some options in the third argument which are value, configurable, enumerable, writable these are explained in this post.

Syntax

Object.defineProperty(object,property_name, property_options);

This method returns the object with the applied property.

Options

The object: This is the reference to which object the property must be applied. If the name of the property already exists then Cannot redefine property exception is thrown.

The property_name: Objects store data in key value pair. When we specify property_name it’s store as the key of that object with a value which we specify in the property_options argument.

The property_options: The above-discussed options such as value, configurable, enumerable, writable can be specified.
Only the value attribute is necessary as it is the value of the property_name. Property description must be an object exception is thrown when we do not pass the third argument and when we have an empty object the value of property_name will be undefined.

Let us look at the below example where we add new property age to a person object.

var person = {"first_name" : "John", "last_name" : "Connar"};
Object.defineProperty(person, "age", {value : 21, writable : true});
console.log(person);
//Console Output
{first_name: "John", last_name: "Connar", age: 21}
Adding new property using Javascript Object.defineProperty() method
Adding new property using Javascript Object.defineProperty() method

After displaying person the object we can see age property added to the end of the object. Look at the third argument were we passed value key in that object we can also pass options such as writable if set to true then it will update the value of property to do that by assigning a new value to object property.

person.age = 18; 

Property Options

value Attribute

The attribute value specifies the initial value of the property.

writable Attribute

The attribute value specifies whether property value can be modified or not. It takes a boolean value. If its value is set to true then the property value will be modified and if set to false it value during assigning will be ignored. Its default value is set to false.

configurable Attribute

By default, the configurable attribute is set to true. This specifies whether property attributes can be modified or not.

var fruit = {};

Object.defineProperty(fruit, "name", {
    value : "Apple",
    configurable : false,
});

fruit.name = "Orange";
console.log(fruit);

//Console Output
{name: "Apple"}

enumerable Attribute

By default, the enumerable attribute is set to true. When set to false the property does not show up as key when called by Object.key() method.

var fruit = {};
Object.defineProperty(fruit, "name", {
    value : "Apple",
    enumerable : true,
});

Object.defineProperty(fruit, "color", {
    value : "Red",
    enumerable : false,
});

console.log(Object.keys(fruit));

//Console Output
["name"]

Conclusion

So it’s time to say bye we have come to the end of our post on Learn Javascript Object.defineProperty() method for Beginners. If you like our content then please do share it and comment to clarify your doubts.

Related Posts

Summary
Review Date
Reviewed Item
Learn Javascript Object.defineProperty() method for Beginners
Author Rating
51star1star1star1star1star
Software Name
Javascript Programming
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development