I add some utils methods to Object class:
Object.prototype.doSomething = function() {
// do something
};
And now I want to prevent any addition to Object.prototype
, is it possible?
Yes, you can make the object non-extensible and make all of its properties read-only via Object.freeze
.
Object.freeze(Object.prototype);
I'd test thoroughly after doing so. :-)
Side note: I'd strongly recommend not using simple assignment to add properties to Object.prototype
. Extending Object.prototype
at all is usually not recommended, but if you're going to do it, make sure you make the new properties non-enumerable by using defineProperty
:
Object.defineProperty(Object.prototype, "doSomething", {
value: function () {
// do something
}
});