Instruction: Explain how Object.freeze(), Object.seal(), and const differ and provide examples of their use cases.
Context: This question probes into the candidate's understanding of immutability and object configuration in JavaScript.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
Object.freeze() is employed when you want to make an object immutable. This means that after an object is frozen, you cannot add new properties, delete existing properties, or change the values of existing properties. It’s worth noting that this immutability is shallow; if the object contains other objects, those objects can still be modified unless they are individually frozen. For example, in a frontend application, you might freeze a configuration object after initialization to prevent any further alterations, ensuring the app's configuration remains constant throughout its lifecycle.
Object.seal(), on the other hand, allows for a slightly more flexible approach. When an object is sealed, you cannot add or remove properties, but you can still change the values of existing properties. This can be particularly useful when you want to lock down the structure of an object but still need to...