Is there anything wrong with calling Object.freeze
on a constructor's prototype in JavaScript?
I was just thinking that since the prototype is shared amongst all instances, perhaps I can ensure that nobody overwrites anything in the prototype by freezing it, since overwriting something on the prototype affects every instance. So for instance, something like the following:
function MyConstructor() {
// Stuff
}
MyConstructor.prototype = {
method1() {
// more stuff
},
method2() {
// even more stuff
},
someArray: ['some', 'values'],
someValue: 'value'
}
Object.freeze(MyConstructor.prototype);
If you freeze the prototype you also freeze the the objects extending it, so they won't be able to override those properties defined in the prototype
This gist is a test to showcase the problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With