1

My question is Object is last on prototype chain all objects inherit properties and methods from it how it is inherit methods from Function.prototype, why Object.__proto__ === Function.prototype // true, why Object.__proto__ is not null.

  • No normal object inherits from `Object` in javascript. They all inherit from `Object.prototype` (which is also the `Object.getPrototypeOf(Function.prototype)`). You will find that `Obect.getPrototypeOf(Object.prototype) === null` as expected. – Bergi Sep 09 '20 at 20:40

2 Answers2

0

The fundamental Object in JavaScript has to have a way for it to be instantiated, so it requires a prototype object to aid in lines like this:

let myObj = new Object();

And that is why Object.__proto__ is not null.

The Function object is a special type of object that facilitates object instantiation via a constructor and when used this way is known as a "constructor function". So, it makes sense for Object to inherit from a Function object so that object instances can be made.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Function is also an object. So it inherits from Object too.

But don't be confused by Object.__proto__. That's something the browser adds and isn't actually part of the language. So don't rely on it or use it.

Essentially, Function.prototype is Object.__proto__.

Geuis
  • 41,122
  • 56
  • 157
  • 219
  • why Function is not last on prototype chain ? – javascript lover Sep 09 '20 at 20:40
  • 1
    Because Function inherits from Object. – Geuis Sep 09 '20 at 20:44
  • No, `Object` inherits from `Function`, which is implemented as native code. This is how/why objects have the ability to be called with the `new` keyword as constructor functions. Open your browser console and type: `Function.prototype;` and see what you get. – Scott Marcus Sep 09 '20 at 20:47
  • @ScottMarcus What Geuis meant is that Function is a subclass of Object, and all functions are objects. No, `Object` does not inherit from `Function`. At best, it does inherit from `Function.prototype`, and it's an `instanceof Function`. – Bergi Sep 09 '20 at 21:03
  • @ScottMarcus riiight. Bit rusty, I've been off work for a few months. Sorry. Yeah, Object does inherit from Function, which is why you can do `new Function()` and `new Object()`. But @Bergi is clarifying what I was trying to say originally. – Geuis Sep 09 '20 at 21:04