Hello
Hello

Reputation: 2265

javascript prototype constructor confusion

function Test()
{
    this.name = 'test name';
}


console.log(Test.prototype.constructor.prototype.constructor);

I can't understand why is this a infinite chain of constructor - prototype?

I mean which are the purpose of this chain and why there is no end, the prototype has a constructor and the constructor has a prototype, its a loop-chain, and the constructor everytime is the same, can't imagine...

Upvotes: 1

Views: 157

Answers (2)

Sascha Krause
Sascha Krause

Reputation: 307

This is a desired behavior. The constructor property of a prototype refers to the object that owns the prototype (so it refers back to itself).

Upvotes: 0

jAndy
jAndy

Reputation: 235982

Well, every Function Object, by default has a .prototype property, which references the prototype object for this function (becomes only important if used as constructor).

And every prototype object by default has a reference to the constructor function, which of course, points back, to the constructor function (in your case Test()).

So, here we go

Test.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor

Upvotes: 3

Related Questions