4

I've been reading about javascript prototype chaining and, as I understood, there is one global Object.prototype which is the base for other prototypes, such as Array.prototype, which can be the base for another prototype. Just like inheritance in class-based OOP. That's fine.

Now, I want to check and compare prototypes of distinct objects. If Array's prototype is based on Object.prototype, I guess that something like Array.prototype.prototype should be possible. But it's undefined:

> Array.prototype.prototype
undefined

And when I type __proto__ instead of prototype, I get:

> Array.__proto__
[Function: Empty]
> Object.__proto__
[Function: Empty]
> Array.__proto__.__proto__
{}

(console output is taken from nodejs). I've got following questions:

  • how can I access "parent prototype" of a prototype?
  • what's the difference between prototype and __proto__?
ducin
  • 25,621
  • 41
  • 157
  • 256

1 Answers1

4

I believe you're looking for:

Object.getPrototypeOf(Array.prototype);
// The same as Object.prototype

(Which is a ES5 feature, not compatible with some older browsers).

what's the difference between prototype and __proto__

The prototype property always belongs to a constructor function (like Object, Array, and custom constructors). The __proto__ property exists on instances created with such constructors, and points to the same object as constructor.prototype.

For example:

function MyClass(){}
var myObj = new MyClass();
myObj.__proto__ === MyClass.prototype; // true

In the examples you gave, Array.__proto__ is actually the prototype object of the constructor function – not its prototype property. That's why it's [Function: Empty], because Array is a function, an instance of the default Function constructor. The __proto__ of some specific array instance is the same as Array.prototype:

var arr = [];
arr.__proto__ === Array.prototype; // true
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Not exactly. `Array.prototype` itself is "*the same as the `__proto__` of some array instance*", not its prototype. – Bergi Jun 26 '13 at 20:35
  • @Bergi If that's about the comment that was on my first code block, you're right. Removed. – bfavaretto Jun 26 '13 at 21:08