Preventing modification of Objects
There are many ways to lock an object according to your needs.
- Object.freeze()
- Object.seal()
- Object.preventExtensions()
Object.freeze()
To safeguard JavaScript objects from modifications, a recommended approach involves employing the Object.freeze() method. By applying this method, the addition of new properties to an object is prohibited, and any endeavor to modify or eliminate existing properties is thwarted. Such attempts are rendered ineffective, leading to either a discreet failure or the triggering of a TypeError exception. This outcome is particularly prevalent when operating within the confines of strict mode, although not exclusively so.
SyntaxThe process of object freezing proves valuable in the representation of logically immutable data structures, particularly when altering object properties could potentially result in adverse effects within your application. It's noteworthy that the application of Object.freeze(…) is limited to a shallow level. Consequently, object values within a frozen object remain susceptible to mutation.
Object.seal()
Object.seal extends the functionality of Object.preventExtensions by not only preventing the addition of arbitrary properties to objects but also by prohibiting the modification of attributes associated with existing properties. Furthermore, Object.seal disallows the deletion of properties altogether.
SyntaxObject.preventExtensions()
The utilization of Object.preventExtensions() involves setting the extensible attribute of the object 'o' to false, effectively preventing the addition of new properties to it. This alteration is irreversible, as once an object's extensibility has been revoked, it cannot be reconfigured to be extensible again.
exampleConclusion
Object modification can be prevented using methods like Object.freeze(), which makes objects effectively immutable by disallowing addition, removal, or alteration of properties. Object.preventExtensions() stops the addition of new properties while maintaining existing ones, while Object.seal() goes a step further by prohibiting both addition and modification of properties.
- JavaScript Interview Questions (Part2)
- JavaScript Interview Questions (Part3)
- Is JavaScript a true OOP language?
- Advantages and Disadvantages of JavaScript
- Difference Between JavaScript and ECMAScript?
- What is noscript tag?
- Escaping Special Characters in JavaScript
- What is undefined x 1 in JavaScript?
- Logical operators in JavaScript
- Difference between '=', '==' and '===' operators in JS
- How to loop through objects in JavaScript?
- How to write html code dynamically using JavaScript?
- How to add html elements dynamically with JavaScript?
- How to load another html page from javascript?
- What Is The Disadvantages Using InnerHTML In JavaScript?
- What is Browser Object Model
- How to detect the OS on the client machine in JavaScript?
- Difference between window, document, and screen in Javascript?
- Difference between the substr() and substring() in JavaScript?
- How to replace all occurrences of a string in JavaScript?
- Test a string as a literal and as an object in JavaScript
- What is Associative Array in JavaScript
- What is an anonymous function in JavaScript?
- What is the use of 'bind' method in JavaScript?
- Pure functions Vs. Impure functions in javascript
- Is Javascript a Functional Programming Language?
- What's the Difference Between Class and Prototypal Inheritance?
- Javascript, Pass by Value or Pass by Reference?
- What is 'this' keyword in JavaScript?
- How Does Function Hoisting Work in JavaScript?
- What do mean by NULL in Javascript?
- What does the delete operator do in JavaScript?
- What is the Infinity property used for in Javascript?
- Event bubbling and Event Capturing in JavScript?
- What is "strict mode" and how is it used in JavaScript?
- What is the difference between call and apply in JavaScript
- Entire content of a JavaScript source file in a function block?
- What is an immediately-invoked function expression?
- What is escape & unescape String functions in JavaScript?
- Instanceof operator in JavaScript
- What Are RESTful (REpresentational State Transfer)Web Services?
- What is Unobtrusive JavaScript & Why it's Important?
- What Does JavaScript Void(0) Mean?
- What are JavaScript Cookies?
- Difference between Client side JavaScript and Server side JavaScript
- TypeError: document.getelementbyid(...) is null
- Uncaught TypeError: Cannot read property of undefined In JavaScript
- Null and Undefined in JavaScript