In this post we will be discussing about JavaScript Object Properties. In our previous post Introduction to JavaScript Objects, we have discussed about JavaScript Objects where we have seen that when we create objects enumerable, writable and configurable attributes are being added.
So In this post we will learn about these attributes. You can actually do some pretty good things if you know what properties are and how to use them.
Read Also : Introduction to JavaScript Objects
To start with we will first take a closer look at properties. Every property has a property descriptor which we can use to see the values of the property.
var website = {name: 'Coding Defined', URL: 'www.codingdefined.com' }
var descriptor = Object.getOwnPropertyDescriptor(website,'name')
Will give an output as where name property has value, writable, enumerable and configurable which are set to true by default.
Writable Attribute
It defines that the property value can be changed from its initial value. If its true you can change it, if its false you cannot change it. To change the value of the writable attribute you can use the below command
Object.defineProperty(website, 'name', {writable: flase})
Now if you try to change the name property you will get an error like "Uncaught TypeError: Cannot assign to read only property 'name' of #<Object>".
Enumerable Attribute
By default properties are enumerable that means we can loop over them. If its true you can loop over the properties, if its false you cannot loop over the properties. To change the value of the enumerable attribute you can use the below command. The property will not be shown when you loop over the properties or when you serialize the property when you change the enumerable attribute to false.
Object.defineProperty(website, 'name', {enumerable: false})
Configurable Attribute
Configurable attribute locks down the property to prevent certain attributes to being changed. It also prevent the property to be deleted from the object. To change the value of the configurable attribute you can use the below command.
Object.defineProperty(website, 'name', {configurable: false})
Now if you try to change any of the attributes of the property you will get an error like "Uncaught TypeError: Cannot redefined property: name".
So there are three things which get affected when you make the property non configurable and they are you cannot change the enumerable attribute, you cannot change the configurable property to true and you can delete the property.
Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.
So In this post we will learn about these attributes. You can actually do some pretty good things if you know what properties are and how to use them.
Read Also : Introduction to JavaScript Objects
To start with we will first take a closer look at properties. Every property has a property descriptor which we can use to see the values of the property.
var website = {name: 'Coding Defined', URL: 'www.codingdefined.com' }
var descriptor = Object.getOwnPropertyDescriptor(website,'name')
Will give an output as where name property has value, writable, enumerable and configurable which are set to true by default.
Writable Attribute
It defines that the property value can be changed from its initial value. If its true you can change it, if its false you cannot change it. To change the value of the writable attribute you can use the below command
Object.defineProperty(website, 'name', {writable: flase})
Now if you try to change the name property you will get an error like "Uncaught TypeError: Cannot assign to read only property 'name' of #<Object>".
Enumerable Attribute
By default properties are enumerable that means we can loop over them. If its true you can loop over the properties, if its false you cannot loop over the properties. To change the value of the enumerable attribute you can use the below command. The property will not be shown when you loop over the properties or when you serialize the property when you change the enumerable attribute to false.
Object.defineProperty(website, 'name', {enumerable: false})
Configurable Attribute
Configurable attribute locks down the property to prevent certain attributes to being changed. It also prevent the property to be deleted from the object. To change the value of the configurable attribute you can use the below command.
Object.defineProperty(website, 'name', {configurable: false})
Now if you try to change any of the attributes of the property you will get an error like "Uncaught TypeError: Cannot redefined property: name".
So there are three things which get affected when you make the property non configurable and they are you cannot change the enumerable attribute, you cannot change the configurable property to true and you can delete the property.
Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.
hi,
ReplyDeleteNice Article...
Its very helpful...
Thank you for sharing...