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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Nicely explained object.freeze. Keep it up.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
amazing post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Nice post about java script. Good work about programming. Upvote and resteem the post.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
interesting, i do programming myself, but today i learned something, thanks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Informative content on Java, Thanks for sharing this post.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Sir would you please share a post about bitcoins price and future. Bcz its current value is less. Will it increase??
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you, @saddam1210. Personally, I think the price will increase, but I am not an expert in this field.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I followed u as a teacher.thnx for ur opinion
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Very useful post for me. your post becomes more easier when you give an example like this. Thank you.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
beshkaf boro jolo
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks.
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Nice post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
fabulous one.....
thanks for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
that's pretty cool to know
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Great one @ghasemkiani.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I am agree with you it's really great
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
java is so helpful operating system
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Wow Wonderful .... Gread post...
i like your post dear @ghasemkiani .........
good programming sharing dear
it is the bless of technology
carry on your activity
#resteemed....
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
nice post. love to read it. it so helpful for me.
thanks for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
you previous and present all post and projects interesting for me... i impressed.... carry on....
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
fabulous one
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
love to read it
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
so helpful post for me
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Wow Wonderful photograpy .... Gread post...
i like your post dear @ghasemkiani .........
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This post ll be really helpful for programners.. Thanx for sharing valueabke infrmation
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Great post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
you are really good at javascript,i also do some programing on c language
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
javascript is awsome, i try out this
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
excelente posts gracias por publicarlas
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
want to learn this boss ..
please support it
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
this is very informative blog .thanks to you for sharing..i praise
you..
Carry on
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This very nice post i appreciate your technology thanks for sharing.. best of luck.. resteemit..
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Its java and its always hangs me.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for valuable post sharing....i like this post...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
upvote and resteem has been done
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
good programming sharing dear
it is the bless of technology
carry on your activity
resteemit done
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Wow!!!! Amazing photography
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
JS programming lessons you have been teaching us is really cool.
Thanks for sharing :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
oh man, thank you so much for providing JS tutorials for us.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
i don't understand java language
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
very well post dear @ghasemkiani
i like your post,, thanks for sharing with us,,,
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
good post thanks for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
good post thanks for sharing
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit