Reputation: 683
Does Object.freeze()
freeze the prototype of an object if you freeze one of the objects instances?
Upvotes: 2
Views: 640
Reputation: 683
class Freeze {
constructor(name) {
this.name = name
}
}
const cold = new Freeze("Justin is cool")
Object.freeze(cold)
Freeze.prototype.size = 666
cold.size // 666
freezing one of the objects instances does not freeze the prototype of the class/constructor it only freezes the instance.
Upvotes: 2
Reputation: 4101
of course when you freeze an object also its prototypes freeze because a frozen object can no longer be changed. We cannot add, edit, or remove properties from it. for example:
const counter = Object.create(counterPrototype);
counter.count = 0;
counter.increment = function(){
console.log('increment')
}
//Cannot assign to read only property 'increment' of object
console.log(counter.increment());
//1
As you can see now that the prototype is frozen we are not able to change the increment()
method in the counter
object.
The prototype is usually used to keep the methods that are shared between different objects.
Freezing the prototype does not allow us to change those properties in the objects inheriting from that prototype. The other properties can be changed.
Upvotes: 1