xxxxxxxxxx
const obj = {
prop1: 'value1',
prop2: 'value2'
};
// Freeze the object
Object.freeze(obj);
// Trying to modify a property will have no effect
obj.prop1 = 'new value';
// Trying to add a new property will have no effect
obj.prop3 = 'value3';
console.log(obj.prop1); // Output: "value1"
console.log(obj.prop3); // Output: undefined
xxxxxxxxxx
const obj = {
prop: 42
};
Object.freeze(obj);
obj.prop = 33;
// //Throws an error in strict mode.
console.log(obj.prop);
// expected output: 42
xxxxxxxxxx
Object.freeze() makes an object immune to everything
even little changes cannot be made.
Object.seal() prevents from deletion of existing properties
but cannot prevent them from external changes
xxxxxxxxxx
const object = {
name = "John",
};
object = "Test"; // Doesn't work, constant makes it so you can't reasign it
object.name = "Doo"; // This works
Object.freeze(object)
object.name = "Foo"; // No longer works