In the previous post, I wrote about Object.preventExtensions
. That functions prevents the addition of new properties to an object. However, it does not prevent you from configuring, mutating, or even deleting the existing properties.
The following code snippet demonstrates the workings of Object.preventExtensions
:
let myObj = {
name: "Ali",
};
Object.preventExtensions(myObj);
// You can still configure the existing properties
Object.defineProperty(myObj, "name", {
get() {
return "Susan";
},
});
console.log(myObj.name); // Susan
// You can even delete the existing properties
delete myObj.name;
console.log(myObj.name); // undefined
// You just can't add new properties.
// The following code throws an exception:
// TypeError: Cannot define property age, object is not extensible
Object.defineProperty(myObj, "age", {
value: 19,
});
What if we want to prevent the existing properties from being configured again?
For this purpose, you can use Object.seal
. This function prevents adding new properties and prevents the existing properties from being reconfigured or deleted. After calling this function, the only thing you can do is to change the values of the existing properties. You cannot change their definition. For example, you cannot convert a data property into an accessor property or vice versa.
Here is an example:
let myObj = {
name: "Ali",
};
Object.seal(myObj);
console.log(Object.isSealed(myObj)); // true
// You cannot add new properties. This fails silently.
myObj.age = 19;
console.log(myObj.age); // undefined
// You cannot reconfigure existing properties.
try {
Object.defineProperty(myObj, "name", {
get() {
return "Susan";
},
});
console.log(myObj.name);
} catch (e) {
console.log(e); // TypeError: Cannot redefine property: name
}
// You can still change property values.
myObj.name = "Susan";
console.log(myObj.name); // Susan
// This is also okay (since this only changes the value of the property
// and does not change its configuration):
Object.defineProperty(myObj, "name", {
value: "Aria",
});
console.log(myObj.name); // Aria
// You can add new properties to the prototype.
myObj.__proto__.age = 14;
console.log(myObj.age); // 14
// You cannot change the prototype.
try {
Object.setPrototypeOf(myObj, {
grade: 9,
});
console.log(myObj.grade);
} catch (e) {
console.log(e); // TypeError: #<Object> is not extensible
}
try {
myObj.__proto__ = {
grade: 9,
};
console.log(myObj.grade);
} catch (e) {
console.log(e); // TypeError: #<Object> is not extensible
}
In short, Object.seal
prevents extensions to the object and makes all the existing properties non-configurable, but does not prevent the values of the existing properties from being changed.
Related Posts
- JavaScript Basics: Object.create
- JavaScript Basics: Object.assign
- JavaScript Basics: Object.getPrototypeOf and Object.setPrototypeOf
- JavaScript Basics: Object.keys, Object.values, and Object.entries
- JavaScript Basics: Object.is
- JavaScript Basics: Object.prototype.hasOwnProperty
- JavaScript Basics: Object.preventExtensions and Object.isExtensible
Very usefully blog
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Very critical for every unknown person
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
so helpful post. i will try it. thanks @ghasemkiani:- for your post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
superb sharing @ghasemkiani . As a #javascript learner your post always help me alot and clear my concept about different issues of JS which i faced often during #programming .
Your posts always help me alot to clear these all issues and i really wait your posts.
As this post clear the issues of javascript and well describe the javascript seal method , my question is , Is javascript freez method is same like javascript seal or both have different techniques and methods.
Thank you @ghasemkiani
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for sharing this post on Java.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanxs alot for sharing with the steemit family its really helpful!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I follow the previous post.this post is like a class basically this very helpful for me.Complete a full part, that will important for all
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
great post dear great writing. i will naver forget you
keep it up dear
@ghasemkiani
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
wow..your writing is very nice dear @ghasemkiani ,, i love your writing,,,,
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I really like this post ghasemkiani! keep it up!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
awesome
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Great
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for update good and informative post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I got some important things from the post, thanks for nice example.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
?????
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Great post as usual. Learning post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
nice share .. thanks for thus us inform ton
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Very important for unknown person
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Programming is a hard work..It is using complex computer work slove ..Carry on dear.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This very nice post i appreciate your post thanks for sharing this technology news carry on my dear friends...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
great post. It so helpful for us.
thanks for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
posting a very good friend
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
helpful post for the programmer or who wants to learn code. Thanks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
very well post and awesome writing
thanks for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
🙏
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thank you sharing for javascript programming.it is a very important part in our life.every people should be know this matter.really I have benefited to read javascript objects. its post to helped doing learning alot of thing.thank you sir for your valuable post..dear@ghasemkiani..
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
very important blog. Your blog is really awesome. I like it. Thanks for sharing.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thankyou for this info :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Again good post from you, thank u
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This is very educative post .Helpful for program learner .keep it up
Thanks a lot @ghasemkiani
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Lots of things to learn from here. Keep it up!
I am on a trip to check on my followers, to comment, upvote and resteem post of 10 follower per day in alphabetical order. Just a means of saying thank you and showing love to the community.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This is good. I love it, more of this.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
very helpfull post...u r great sir..restem done
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
programming is a difficult job.keep it up. one day you will make a good programmer
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
nice bolg on javascript
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit