ES6 : 5 Prevent Object Mutation
As seen in the previous post
declaration alone doesn't really protect your data from mutation. To
ensure your data doesn't change, JavaScript provides a function Object.freeze() to prevent data mutation.
Once
the object is frozen, you can no longer add, update, or delete
properties from it. Any attempt at changing the object will be rejected
without an error.
let obj = {
name:"FreeCodeCamp"
review:"Awesome"
};
Object.freeze(obj);
obj.review = "bad"; //will be ignored. Mutation not allowed
obj.newProp = "Test"; // will be ignored. Mutation not allowed
console.log(obj);
// { name: "FreeCodeCamp", review:"Awesome"}
name:"FreeCodeCamp"
review:"Awesome"
};
Object.freeze(obj);
obj.review = "bad"; //will be ignored. Mutation not allowed
obj.newProp = "Test"; // will be ignored. Mutation not allowed
console.log(obj);
// { name: "FreeCodeCamp", review:"Awesome"}
Comments
Post a Comment