Object.seal() vs Object.freeze()
Rudra Pratap
Posted on May 8, 2023
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
đź’– đź’Ş đź™… đźš©
Rudra Pratap
Posted on May 8, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
react Everything You Need to Know About React useState Hook – Practical Examples Inside
September 30, 2024