Created
November 28, 2020 15:23
-
-
Save vipinrana/a0157d79718d31da431ecf0bfd2a918b to your computer and use it in GitHub Desktop.
Object.freeze() vs Object.seal() vs Object.preventExtensions()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = {id: 1, name: "John Doe", age: 25}; | |
/** | |
* Object.freeze() | |
* | |
* In freeze object, addition, updation and delete operation cannot be done except read | |
* | |
*/ | |
Object.freeze(obj); | |
// update the existing property value | |
obj.age = 23; | |
// adding new property | |
obj.gender = "male"; | |
// deleting name property | |
console.log(delete obj.name); // false | |
console.log(obj); // {id: 1, name: "John Doe", age: 25} | |
/** | |
* Object.seal() | |
* | |
* In sealed object, addition and deletion cannot be done except updation and read | |
* | |
*/ | |
Object.seal(obj); | |
// update the existing property value | |
obj.age = 23; | |
// adding new property | |
obj.gender = "male"; | |
// deleting name property | |
console.log(delete obj.name); // false | |
console.log(obj); // {id: 1, name: "John Doe", age: 23} | |
/** | |
* Object.preventExtensions() | |
* | |
* In prevent extension object, addition cannot be done except updation, deletion and read | |
* | |
*/ | |
Object.preventExtensions(obj); | |
// update the existing property value | |
obj.age = 23; | |
// adding new property | |
obj.gender = "male"; | |
// deleting name property | |
console.log(delete obj.name); // true | |
console.log(obj); // {id: 1, age: 23} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment