How to use Object.seal in JavaScript
Object.seal
When we seal an object:
- We can’t add new properties
- We can’t delete existing properties
- We can modify the existing value
- We can’t convert data properties to accessor properties and vice versa
The sealed state of an object can be checked using Object.isSealed.
Example
Sealing an object
var user = {
set updateName(name) {this.name = name}, // accessor property
name : "John", // data property
age : 23,
get() {
return `My name is ${this.name} my age is ${this.age}`
}
};
console.log("Check if object is sealed", Object.isSealed(user) );
console.log("Sealing object");
let sealedObject = Object.seal(user);
console.log("object sealed");
Adding new a property
We can’t add a new property to the object if the object is sealed.
console.log("Tring to add new property user.weight=100 ");
user.weight = 100;
// this property will not be added
console.log(user, "Weight will not be added because user object is sealed");
Modifying an existing property
We can change the value of an existing property.
console.log("Tring to modify property user.age=24 ");
user.age = 24;
cosole.log(user, "We can modify the value of sealed object");
Deleting an existing property
We cannot delete the existing property.
console.log("Tring to delete property 'delete user.get' ");
delete user.get;
console.log(user, "get property will not be removed from user object");
Changing data properties to accessor properties
We can’t convert data properties to accessor properties and vice versa.
console.log("Converting accessor prop to data prop");
user.updateName = "accessor to data"; //this fails
console.log("Afte modifying user.updateNAme" , user);
console.log("Converting data prop to accessor prop");
try {
Object.defineProperty(obj, 'name', {
get: function() { return "Test"; }
}); // throws a TypeError
} catch(e) {
console.log("cannot convert data to accessor ");
}
var user = {set updateName(name) {this.name = name}, // accessor propertyname : "John", // data propertyage : 23,get() {return `My name is ${this.name} my age is ${this.age}`}};console.log("----------------")console.log("Check if object is sealed", Object.isSealed(user) );console.log("----------------\n")console.log("Sealing object");let sealedObject = Object.seal(user);console.log("object sealed");console.log("----------------\n")console.log("Tring to add new property user.weight=100 ");user.weight = 100;// this property will not be addedconsole.log(user, "Weight will not be added because user object is sealed");console.log("----------------\n")console.log("Tring to modify property user.age=24 ");user.age = 24;console.log(user, "We can modify the value of sealed object");console.log("----------------\n")console.log("Tring to delete property 'delete user.get' ");delete user.get;console.log(user, "get property will not be removed from user object");console.log("----------------\n")console.log("Converting accessor prop to data prop");user.updateName = "accessor to data"; //this failsconsole.log("Afte modifying user.updateNAme" , user);console.log("----------------\n")console.log("Converting data prop to accessor prop");try {Object.defineProperty(obj, 'name', {get: function() { return "Test"; }}); // throws a TypeError} catch(e) {console.log("cannot convert data to accessor ");}