seal() and freeze() are one of the important methods in Object namespace.
Seal
With Object.seal we can prevent new properties from being added, or existing properties to be removed.
However, you can still modify the value of existing properties.
Q) Which of the following will modify the person object?
const person = { name: 'Lydia Hallie' };
Object.seal(person);
- person.name = "Evan Bacon"
- person.age = 21
- delete person.name
- Object.assign(person, { age: 21 })
Answer: 1
Freeze
The Object.freeze method freezes an object. No properties can be added, modified, or removed.
However, it only shallowly freezes the object, meaning that only direct properties on the object are frozen.
Q) Which of the following will modify the person object?
const person = {
name: 'Lydia Hallie',
address: {
street: '100 Main St',
},
};
Object.freeze(person);
- person.name = "Evan Bacon"
- delete person.address
- person.address.street = "101 Main St"
- person.pet = { name: "Mara" }
Answer: 3
Top comments (0)