It's simple:
+-----------------+ tail
| |-----------------> [true]
| benji |
| |
+-----------------+
|
| __proto__
|
v
+-----------------+ constructor +-----------------+
| |------------------>| |
| Dog.prototype | | Dog |
| |<------------------| |
+-----------------+ prototype +-----------------+
|
| __proto__
|
v
+-----------------+ constructor +-----------------+
| |------------------>| |
| Object.prototype| | Object |
| |<------------------| |
+-----------------+ prototype +-----------------+
benji.constructor
is Dog
.
benji.constructor.prototype
is Dog.prototype
.
benji.constructor.prototype.constructor
is Dog
.
benji.constructor.prototype.constructor.prototype
is Dog.prototype
.
benji.constructor.prototype.constructor.prototype.constructor
is Dog
.
This goes on forever. Instead what you want is Object.getPrototypeOf
:
Object.getPrototypeOf(benji).constructor
is Dog
.
Object.getPrototypeOf(Object.getPrototypeOf(benji)).constructor
is Object
.
Since Object.getPrototypeOf
is iterated you could create a special function for it:
function getPrototypeOf(n, object) {
if (n === 0) return object;
else return getPrototypeOf(n - 1, Object.getPrototypeOf(object));
}
Now you can use it as follows:
getPrototypeOf(1, benji).constructor; // Dog
getPrototypeOf(2, benji).constructor; // Object
That's all folks.