January 2020
Intermediate to advanced
470 pages
11h 13m
English
If we want to avoid the possibility of a programmer accidentally or willingly modifying an object, freezing it is a valid solution. After an object has been frozen, any attempts at modifying it will silently fail—JavaScript won't report an error or throw an exception, but it won't alter the object either. In the following example, if we attempt to make the same changes we made in the previous section, they just won't have any effect, and myObj will be unchanged:
const myObj = { d: 22, m: 9 };Object.freeze(myObj);myObj.d = 12; // won't have effect...myObj.m = 4;console.log(myObj);// Object {d: 22, m: 9}
Read now
Unlock full access