Search code examples
javascriptprototype

Why Object.getPrototypeOf is static and not in Object's prototype


I am asking why, to get an object's prototype, it has been implemented getPrototypeOf as a static method on Object's constructor rather than in its prototype?

Is there any reason for JS creators not to do

Object.prototype.getPrototypeOf = function() { return Object.getPrototypeOf(this); };

(which I could do myself but I know it is not good to extend JS standard objects' prototypes)?

[EDIT] Look, I am not proposing to add getPrototypeOf to Object.prototype, just curious about it wasn't done. I believe it was because of possible malfunctioning in specific cases


Solution

  • Putting anything into Object's prototype means adding it to all the objects ever created in all existing and future JS code on earth. Consequences would be very unpredictable. These extra 7 chars - not too high price to pay for stability.

    Well, this method implemented in Object.prototype would be called just getPrototype (as far as getPrototypeOf with no arguments looks a bit strange). It is a very common name and indeed someone already has such method in one of his objects with a totally different meaning. Okay, adding global getPrototype won't break his existing code, but one day I will try to call hisObj.getPrototype assuming the new meaning, and I will get something wrong.

    By the way, the isPrototypeOf method added in 3rd edition of ES is 12 years older than getPrototypeOf defined by ES 5.1.