
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Freeze an Object in JavaScript
In the Real time world javascript doesn't have traditional classes as seen in other languages. It has objects and constructors. Object.freeze() is one among many constructor methods helps to freeze an object.
Freezing an object does not allow new properties to be added to the object and also prevents the object from changing its own properties. Object.freeze() will always try to preserve the enumerability, configurability, writability and the prototype of the object. It won't create a frozen copy.
Applications
1) freeze() is used for freezing objects and arrays.
2) freeze() is used to make an object immutable.
Syntax
Object.freeze(obj)
Example
<html> <body> <script> // an object is created and a value is assigned var myObj1 = { prop1: 'freezed values can not be changed' }; // the created object is freezed var myObj2 = Object.freeze(myObj1); // property of the frozen object is updated myObj2.prop1 = 'change the freezed value'; // Displaying the properties of the frozen object --> document.write(myObj2.prop1); </script> </body> </html>
Outputfreezed values can not be changed
Advertisements