JAVA SCRIPT - Preventing Any Changes to an Object - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript JAVA SCRIPT - Preventing Any Changes to an Object - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, December 27, 2018

JAVA SCRIPT - Preventing Any Changes to an Object

Preventing Any Changes to an Object



Problem

You’ve defined your object, and now you want to make sure that its properties aren’t redefined or edited by other applications using the object. 

Solution

Use Object.freeze() to freeze the object against any and all changes:
'use strict';
var test = {
 value1 : 'one',
 value2 : function() {
 return this.value1;
 }
}
try {
 // freeze the object
 Object.freeze(test);
 // the following throws an error in Strict Mode
 test.value2 = 'two';
 // so does the following
 test.newProperty = 'value';
 var val = 'test';
 // and the following
 Object.defineProperty(test, 'category', {
 get: function () { return test; },
 set: function (value) { test = value; },
 enumerable: true,
 configurable: true
 });
} catch(e) {
 console.log(e);
}

EXPLAIN


 ECMAScript 5 brought us several Object methods for better object management. The least restrictive is Object.preventExtensions(obj), , which dis‐ allows adding new properties to an object, but you can still change the object’s property descriptor or modify an existing property value.

The next, more restrictive method is Object.seal(), which prevents any modifications or new properties from being added to the property descriptor, but you can modify an existing property value. The most restrictive method is Object.freeze().

This method disallows extensions to the object and restricts changes to the property descriptor. In addition, Object.freeze() also prevents any and all edits to existing object properties. Literally, once the object is frozen, that’s it—no additions, no changes to existing properties.

The first property modification in the solution code:

test.value2 = "two";

results in the following error (in Chrome):

TypeError: Cannot assign to read only property 'value2' of #

If we comment out the line, the next object adjustment:

test.newProperty = "value";

throws the following error:

TypeError: Can't add property newProperty, object is not extensible


Commenting out this line leaves the use of defineProperty():

var val = 'test';
// and the following
Object.defineProperty(test, "category", {
 get: function () { return test; },
 set: function (value) { test = value; },
 enumerable: true,
 configurable: true
});


We get the final exception, for the use of defineProperty() on the object:

TypeError: Cannot define property:category, object is not extensible.

If we’re not using strict mode, the first two assignments fail silently, but the use of defineProperty() still triggers an exception (this mixed result is another good reason for using strict mode). Check if an object is frozen using the companion method, Object.isFrozen():

if (Object.isFrozen(obj)) ...

No comments:

Post a Comment

Post Top Ad