0

Is there a rationale for why some of the convenience and reflection methods in JavaScript are defined on Object, whereas others are defined on Object.prototype? For instance, to find out whether an object a is a prototype of object b, you call isPrototypeOf on a:

a.isPrototypeOf(b)

Conversely, to get the prototype of object a, you call getPrototypeOf on Object with a as an argument:

Object.getPrototypeOf(a)

To me, this seems a little counterintuitive, at least at first sight -- I would expect that the characteristics of an object (like its prototype) can be queried starting from the object itself. (In other words, I would tend to expect a getPrototype method on Object.prototype, to be called as a.getPrototype(), rather than a getPrototypeOf method on Object.)

This answer does a fairly good job explaining why you might want to pick one over the other, but the reasoning behind the particular decisions in the language standard (see getPrototypeOf example above) is not entirely clear to me. But I guess there must be one :) What is it?

Community
  • 1
  • 1
dlukes
  • 1,313
  • 16
  • 27
  • 1
    Or maybe [Why were ES5 Object methods not added to Object.prototype?](http://stackoverflow.com/q/9735026/1048572) would've been a better duplicate target – Bergi Jul 01 '16 at 12:35
  • If `Object.getPrototypeOf()` were defined on the prototype then it would be harder to call it on objects that don't have a prototype. – nnnnnn Jul 01 '16 at 12:45
  • @Bergi: ah, thanks a lot, that seems to be what I was looking for :) both links are useful and relevant, but I must have phrased my original question in a way that neither Google nor SO suggested these. – dlukes Jul 01 '16 at 13:25
  • @Bergi and yes, seeing that there's only one possible duplicate target, your second suggestion is the better option I think – dlukes Jul 01 '16 at 13:29
  • @dlukes: Well I struggle to find that post everytime I want to link it as well, I only know that I posted that answer… And the second one I just came across today in the "related questions" sidebar. – Bergi Jul 01 '16 at 13:31

0 Answers0