0
Object.getPrototypeOf(..)

returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object. so, the below makes sense

const prototype1 = {};
const object1 = Object.create(prototype1);

console.log(Object.getPrototypeOf(object1) === prototype1); // logs true

Even this looks fine

function fn() {
}
Object.getPrototypeOf(fn) == Function.prototype // logs true

But I am kind of confused why

Object.getPrototypeOf(Object) == Function.prototype // logs true

Please explain

recmach
  • 65
  • 2
  • 9

2 Answers2

2

Some points to consider:

Object is a function

It can be used to convert a primitive to an object. Like Object("test") will create a String instance.

There is a constructor for functions

The constructor for functions is Function. It is not commonly done, but you can create a function by calling this constructor. For example: new Function("alert('test')") returns a function that when called will display an alert.

Functions which are created with function statements, expressions, arrow function syntax,... also have as constructor this Function. So for instance, eval.constructor === Function is a true expression. The same goes for Object.constructor === Function. You can do this for any function.

Where to find the a function's prototype object?

Like every constructor, Function has a prototype property. That object defines the prototype properties and methods that all functions inherit. We can find that prototype object via Function.prototype, or we can find it by taking a Function instance (i.e. a function), and getting its prototype object. This we do by calling Object.getPrototypeOf on a specific function.

Equalities

And so we can see that all of the following are equal:

var a = Object.getPrototypeOf(eval);
var b = Object.getPrototypeOf(parseInt);
var c = Object.getPrototypeOf(function () {});
var d = Object.getPrototypeOf(RegExp);
var e = Object.getPrototypeOf(Object);
var f = Function.prototype;

// All true:
console.log(a === f, b === f, c === f, d === f, e === f);
trincot
  • 317,000
  • 35
  • 244
  • 286
1

Everything in JavaScript inherits from Object.prototype. That is why everything in JavaScript is said to be objects. Even functions and arrays are objects under the hood.

So Object.prototype is the prototype of:

  • Function.Prototype
  • Array.Prototype

Function.Prototype is the prototype of Function and Array:

Array.prototype.prototype == Function.Prototype // true
Function.prototype.prototype == Function.Prototype // true

And again, Array and Function inherits from Object.prototype:

Object.getPrototypeOf(Array.prototype) == Object.prototype // true
Object.getPrototypeOf(Function.prototype) == Object.prototype // true

So to return to your question. The prototype of Object is Object.prototype. And Object.prototype is the prototype of Function.prototype, thus:

Object.getPrototypeOf(Object) == Function.prototype // true

JavaScript is like a family tree: Object.prototype had a baby called Function.prototype. Function.prototype then had babies of its own called Array and Function. And then follows arrays, classes etc... In the end, everything leads back to Object

Mads Akselsen
  • 249
  • 1
  • 5
  • *"The prototype of Object is Object.prototype. And Object.prototype is the prototype of Function.prototype, thus: `Object.getPrototypeOf(Object) == Function.prototype`"*: this is a faulty reasoning. It is like saying "O is a P, and F is a P, therefore O is a F". The outcome is of course true, but the reasoning is wrong. – trincot Jun 11 '22 at 07:07
  • I agree with your example, because you don't make a connection between O and F. But that is not the case in my example. In my reasoning, I make a direct connection between `Object` and `Function`. I wrote: "Object.prototype is the prototype of Function.prototype" which directly translates to `Object.getPrototypeOf(Object) == Function.prototype`. The reason I write "The prototype of Object is Object.prototype" is due to OP asking why `prototype` of `Object` equals the `prototype` of Function – Mads Akselsen Jun 11 '22 at 07:21
  • 1
    *""Object.prototype is the prototype of Function.prototype" which directly translates to"*: eh, that phrase would translate to `Object.prototypeOf(Function.prototype) === Object.prototype`. You are also conflating `Object.prototype` with `Object.getPrototypeOf(Object)` which are two unrelated things. – trincot Jun 11 '22 at 07:35
  • Yeah, good point. So to clear up any confusions I've made: `.prototype` will get the prototype of a constructor function's instances. `Object.getPrototypeOf(object)` will get the prototype of the object, which you can also get from `myConstructor.__proto__`. So when you do `myConstructor.prototype`, you're not getting the prototype of `myConstructor`, you're getting the prototype of `myConstructor`'s instances instead. Correct? :-) – Mads Akselsen Jun 11 '22 at 07:51
  • Source: https://stackoverflow.com/questions/38740610/object-getprototypeof-vs-prototype – Mads Akselsen Jun 11 '22 at 08:00
  • *"`Object.getPrototypeOf(object)` will get the prototype of the object, which you can also get from `myConstructor.__proto__`"*: No, `myConstructor.__proto__` is the prototype object from which `myConstructor` was created, i.e. `Function.prototype`. It has nothing to do with `Object.getPrototypeOf(object)`. – trincot Jun 11 '22 at 08:50
  • You can get the `prototype` from which a constructor was created from both `Object.getPrototypeOf` and `.__proto__`: `Object.getPrototypeOf(Function) == Function.__proto__ // true` – Mads Akselsen Jun 11 '22 at 09:01
  • My comment was about your claim which I quoted, which is false. Now you write something different again, which is true. Let me refer to [an answer I gave on this subject](https://stackoverflow.com/questions/572897/how-does-javascript-prototype-work/42880438#42880438). Maybe it helps. – trincot Jun 11 '22 at 09:14