Reputation: 45
I created a class Money intended to be immutable as follow
class Money{
constructor(currency, amount)
{
// this.curre...
Object.freeze(this) // new instances are immutable
}
}
// after that i want the [[Prototype]] of class.prototype to be null
Object.setPrototypeOf(Money.prototype,null)
// to be sure that my class's prototype is not going to be changed
Object.freeze(Money)
but
Object.setPrototypeOf(Money.prototype, {}) //
console.log(Money.prototype) // I got {}
// I must add Object.freeze(Money.prototype) ???
prototype isn't really property of a class / Constructor function?
Upvotes: 0
Views: 58
Reputation: 413737
Object.freeze()
only affects the object you pass to it. It does not transitively freeze all objects that are values of properties.
If you have an object a
:
let a = {
b: {
c: "Hello";
}
};
then it would be pretty surprising if Object.freeze(a)
made it impossible to change properties of a.b
, because that's a different object.
Beyond that, it would be vastly more surprising if freezing one instance of a constructor would also result in the prototype of the constructor to be frozen.
Upvotes: 2