InterviewSolution
Saved Bookmarks
| 1. |
What is the difference between const and Object.freeze(). |
|
Answer» Const is a property that applies to bindings ("variables"). It creates an immutable binding, which means you can't change its value. const a = { b: "apple"};let c = { d: "mango"};a = c; // ERROR "a" is READ-onlyObject.freeze() is a function that works with values, primarily object values. It makes an object immutable, meaning that its properties cannot be changed. freeze() returns the same object that was supplied to it as a PARAMETER. It does not make a frozen COPY. If the parameter to this method is not an object (a primitive) in ES5, a TypeError will occur. A non-object argument in ES6 will be treated as if it were a frozen regular object and will be returned. let a = { b: "apple"};let c = { d: "mango"};Object.freeze(a);a.b = "KIWI"; //TypeError: Cannot ASSIGN to read only property 'name' of objectconsole.log(a); |
|