0

I'm confused with javascript's Object.prototype and Anything.prototype.

In chrome console, i have code below:

enter image description here

My questions:

  1. The default prototype for any function is an instance of Object. Is it right?
  2. If 1 is true. So, both Anything.prototype and Object.prototype are an instance of Object. Object.prototype is an instance with its __proto__ === null. Therefore, Object.prototype is a special instance at the top of prototype chain. Can i understand like this?
Gary Chen
  • 105
  • 6

1 Answers1

2

The default prototype for any function is an instance of Object.

Yes, the .prototype property of any function holds an object that inherits from Object.prototype:

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

So, both Anything.prototype and Object.prototype are an instance of Object.

I would not say that Object.prototype is an instance of Object, since it defines what that is - and it does not inherit from Object.prototype, as you say yourself:

Object.getPrototypeOf(Object.prototype) === null // true
Object.prototype instanceof Object // false, because
Object.prototype.isPrototypeOf(Object.prototype) // false (obviously)

Therefore, Object.prototype is a special instance at the top of prototype chain.

Yes, though I'd say the top of every prototype chain is null.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Specifically, `Object.prototype` is [...an immutable prototype exotic object...](http://www.ecma-international.org/ecma-262/7.0/index.html#sec-properties-of-the-object-prototype-object) whose \[\[Prototype\]\] internal slot is `null`. So indeed, not an instance of `Object`, although it's an object. – T.J. Crowder Mar 18 '17 at 17:23
  • @T.J.Crowder Being exotic doesn't mean it can't be an `instanceof Object`, but yes, it's very special indeed. – Bergi Mar 18 '17 at 19:52
  • I didn't say it did. But with its [[Prototype]] slot being null, it's not an instanceof Object. – T.J. Crowder Mar 18 '17 at 21:55
  • Ah, I thought you had the emphasis on the link, since I already stated in my answer that it's `null`. But enough misunderstandings on this page! – Bergi Mar 18 '17 at 22:02