When you realize how to make an object, you may wish to find out about item property descriptors. As a recap, let us state you have an object called cat:
Each object property contains more information than just a value. For example, you can print other property information using the
On the console, you can see that a property name offers further information:
As is very clear, if writable is set to true, you can rewrite a property value, etc.
As of now, you know about object property descriptors, so if you are required to make a property Read-Only, you will set the property writable to false.
- Prevent an object from having a new property.
- In addition to requirement 1, mark all properties configurable to false.
- In addition to requirement 2, make all properties writable to false.
Starting with ECMA 6, you have methods to achieve the above requirements. Let’s look at them one by one:
Let’s say you have an object called
To prevent the default behavior from adding properties dynamically in an object, you need to use
Object.preventExtensions(). This method prevents an object from having new properties added to it.
In strict mode, you will get an error with very clear messaging that states, “cannot add property, object is not extensible.”
To summarize, you should use the
object.preventExtensions method to prevent an object from having new properties added to it.
Let’s say you want to seal an object, meaning:
- You should be unable to add new properties (calling
- No configuration should be changed (setting configurable properties to false).
You can seal an object by using the
object.seal() method. Let’s again consider the
You don’t want new properties added to
cat and the configurable of all properties should be set to false. You can do this using the
Since you have a sealed object, as an output, you will get undefined and configurable set to false.
To summarize, you should use
Object.seal() to seal an object. You will be unable to add new properties and configurable is set to false.
Let us say you want to freeze an object, meaning:
- You should be unable to add new properties, meaning calling
- No configuration should be changed (setting configurable of properties to false).
- For all properties, writable should be set to false.
You can freeze an object by using the
Object.freeze() method. It essentially calls the
Object.seal() method and sets the writable property to false.
Let’s again consider the
New properties should not be added to to the object, configurable of all properties should be set to false, and writable of properties should be set to false. You can do this using the
Since you have frozen the object, as an output, you will get undefined, 9, configurable, and writable set to false.
To summarize, you should use
Object.freeze() to freeze an object. Once you freeze an object, you should not able to add new properties or rewrite the value of a property, and configurable will be set to false.
Object.freeze are very much required. I hope you now have a better grasp of these concepts.