4

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

Nillus
  • 1,131
  • 1
  • 14
  • 32
  • Very interesting... – ibrahim mahrir Nov 25 '17 at 02:48
  • 1
    There are objects that don’t inherit `Object.prototype`, like `Object.create(null)`. (`isPrototypeOf` is on `Object.prototype`, by the way, but I would consider that a mistake.) – Ry- Nov 25 '17 at 03:33
  • TC39 hasn't touch `Object.prototype` in a long time. Literally the only updates we ever got to it were `hasOwnProperty`, `propertyIsEnumerable` and `toLocaleString` 18 years ago. – MinusFour Nov 25 '17 at 03:47

1 Answers1

1

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.

Ilya Myasin
  • 151
  • 1
  • 6
  • it is not about saving characters, it is about curiosity, theory quesion. Anyway I can't see any stability issue since a previously non existant method is not used in past code - adding previously unavailable method to old code causes no issues since nobody used it, imho. – Nillus Nov 25 '17 at 03:17
  • Exactly, and isPrototypeOf is in fact in the Object.prototype! – Nillus Nov 25 '17 at 03:19
  • Well, imagine that one day condition `if (typeof x.y !== 'undefined')` becomes `true` for any `x`. Yes, the `isPrototypeOf` is not too common name, but it was not ever marked as reserved word. – Ilya Myasin Nov 25 '17 at 03:26
  • So, i suppose 12 years later language architects decided that putting methods of this sort into Object's prototype is not a good idea :) – Ilya Myasin Nov 25 '17 at 03:27
  • I do not understand why. Aren't prototypes there actually for that purpose? I don't understand the typeof example either – Nillus Nov 25 '17 at 03:29
  • `hasOwnProperty` mentioned in the other answer (i have not enough reputation to comment there) is from ES3 spec too. – Ilya Myasin Nov 25 '17 at 03:30
  • Prototypes are actually for that purpose, but only in the userland. `typeof x.y !== 'undefined'` is a very common (while not the best) way to prevent the `Cannot read property ... of undefined` error, so if someone used `getPrototypeOf` as `y` bringing `getPrototypeOf` into Object's prototype will break his code. – Ilya Myasin Nov 25 '17 at 03:39
  • I understand stability worries, but this cannot be the only worry, otherwise ES spec cannot add anything to existing standard object. Of course only useful features justify heavy changes, and this is not the case you're right; however I think the case of check for `getPrototypeOf` against `undefined` is not very common. – Nillus Nov 25 '17 at 03:46
  • New ES specifications actually DO NOT add anything to existing standard object. – Ilya Myasin Nov 25 '17 at 04:02
  • I've updated my answer with a little-closer-to-real-life example. – Ilya Myasin Nov 25 '17 at 04:28