In the previous posts, I discussed about two levels of object protection: Object.preventExtensions
and Object.seal
. The last level of protection is Object.freeze
, which also makes all the properties unchangeable. You cannot add a new property, you cannot delete an existing property, you cannot reconfigure it, and you cannot change its value.
The following code snippet demonstrates the effects of Object.freeze
:
let myObj = {
name: "Ali",
};
Object.freeze(myObj);
console.log(Object.isFrozen(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 cannot change property values.
myObj.name = "Susan";
console.log(myObj.name); // Ali
try {
Object.defineProperty(myObj, "name", {
value: "Aria",
});
console.log(myObj.name); // Ali
} catch(e) {
console.log(e); // TypeError: Cannot redefine property: name
}
// 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.freeze
prevents extensions to the object and makes all the existing properties non-configurable, and also prevents the values of the existing properties from being changed. In this way, the object becomes immutable.
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
- JavaScript Basics: Object.seal and Object.isSealed
well thanks for sharing .. learning
Nicely explained object.freeze. Keep it up.
JavaScript is a great program language.....and i like your post very much..... i waiting for your next post....i wish for your best of luck brother
amazing post
Nice post about java script. Good work about programming. Upvote and resteem the post.
interesting, i do programming myself, but today i learned something, thanks
Sir would you please share a post about bitcoins price and future. Bcz its current value is less. Will it increase??
Thank you, @saddam1210. Personally, I think the price will increase, but I am not an expert in this field.
I followed u as a teacher.thnx for ur opinion
Informative content on Java, Thanks for sharing this post.
Very useful post for me. your post becomes more easier when you give an example like this. Thank you.
beshkaf boro jolo
Thanks.
.. ممنون از اشترک این پست، خصیصه ی مفیدی بنظر میاد
میشه گفت باعث افزایش امنیت میشه و سمت کلاینت به حفاظت از داده ها کمک میکنه؟
Nice post
fabulous one.....
thanks for sharing
that's pretty cool to know
Great one @ghasemkiani.
I am agree with you it's really great
good programming sharing dear
it is the bless of technology
carry on your activity
resteemit done
Wow Wonderful photograpy .... Gread post...
i like your post dear @ghasemkiani .........
this is very informative blog .thanks to you for sharing..i praise
you..
Carry on
Wow Wonderful .... Gread post...
good programming sharing dear
it is the bless of technology
carry on your activity
i like your post dear @ghasemkiani ......... #resteemed....
you previous and present all post and projects interesting for me... i impressed.... carry on....
This very nice post i appreciate your technology thanks for sharing.. best of luck.. resteemit..
Its java and its always hangs me.
Thanks for valuable post sharing....i like this post...
Wow!!!! Amazing photography
I don't actually think it's a lot of people. I think it's a vocal minority. And it turns out that the most vocal, such as Mattias and Elliott, don't actually understand classes, inheritance or composition nearly as well as they think they do here is last prog which i tried.
class Person {}
// A student IS-A person; inheritance seems reasonable class Student extends Person {}
// An employee IS-A person; inheritance seems reasonable class Employee extends Person {}
// Oops. What if someone is *both* a student and an employee?~
// If we're in a language that allows only single inheritance, then we're just sunk// And even if we're in a language that allows multiple inheritance,
// we may run into ambiguity issues since we've inherited Person twice
class StudentEmployee extends Student, Employee {} ???` @ghasemkiani
i like your post,, thanks for sharing with us,,,very well post dear @ghasemkiani
nice post. love to read it. it so helpful for me.
thanks for sharing
fabulous one
love to read it
so helpful post for me
java is so helpful operating system
This post ll be really helpful for programners.. Thanx for sharing valueabke infrmation
Great post
you are really good at javascript,i also do some programing on c language
javascript is awsome, i try out this
excelente posts gracias por publicarlas
want to learn this boss ..
please support it
upvote and resteem has been done
JS programming lessons you have been teaching us is really cool.
Thanks for sharing :)
oh man, thank you so much for providing JS tutorials for us.
i don't understand java language
good post thanks for sharing
good post thanks for sharing