

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to prevent modification of object in JavaScript ?.
ECMAScript 5 has introduced several methods to prevent modification of object. Those preventive measures ensures that no one, accidentally or otherwise change functionality of object.
There are 3 levels of preventive methods
1) Prevent Extensions
In this level, one cannot add any new properties or methods but can access existing properties or methods. Here there is an ability to delete the respective object. Object.preventExtensions() is the method used to accomplish this task. It prevents any new properties from ever being added to the object.
Example
<html> <body> <script> var object1 = { prop1: 1 }; Object.preventExtensions(object1); delete object1.prop1 // value got deleted try { Object.defineProperty(object1, 'prop2', { value: 2 }); } catch (err) { document.write(err); } document.write("</br>"); document.write(object1.prop1); </script> </body> </html>
Output
TypeError: Cannot define property prop2, object is not extensible undefined // deleted so undefined
2) Seal
It is same as preventing extensions, in addition it doesn't allow to delete existing properties or methods. To accomplish this task Object.seal() method is used.
Example
<html> <body> <script> var object1 = { prop1: 1 }; Object.seal(object1); object1.prop1 = 2; // value got changed delete object1.prop1; try { Object.defineProperty(object1, 'prop2', { value: 2 }); } catch (err) { document.write(err); } document.write("</br>"); document.write(object1.prop1); // it gives value as 2 because of seal. </script> </body> </html>
Output
TypeError: Cannot define property prop2, object is not extensible 2 // because of seal the value can't be deleated but got updated
3) Freeze
In addition to seal's functionality, freeze doesn't allow even to access the existing properties. To freeze an object we use Object.freeze() method. It can also make an object immutable.
Example
<html> <body> <script> var object1 = { prop1: 1 }; Object.freeze(object1); object1.prop1 = 2; // value got updated delete object1.prop1; // value got deleted try { Object.defineProperty(object1, 'prop2', { value: 2 }); } catch (err) { document.write(err); } document.write("</br>"); document.write(object1.prop1); // it gives 1 as output despite value updated to 2 </script> </body> </html>
Output
TypeError: Cannot define property prop2, object is not extensible 1 // because of freeze the value won't get delete and won't get update.
- Related Questions & Answers
- How to prevent object of a class from garbage collection in Java?
- How to prevent duplicate JavaScript Variables Declaration?
- How to use comments to prevent JavaScript Execution?
- How to print content of JavaScript object?
- How to transform object of objects to object of array of objects with JavaScript?
- How to programmatically prevent scrolling in WebView of iOS?
- How to prevent MongoDB from returning the object ID while finding a document?
- How to disallow altering of object variables in JavaScript?
- How to prevent gestational diabetes?
- How to prevent data corruption
- How to duplicate Javascript object properties in another object?
- How to print the content of JavaScript object?
- Final state of the string after modification in Python
- How to get creation and modification date/time of a file using Python?
- How to set creation and modification date/time of a file using Python?