Firebug showing infinite recursion for constructor

I wanted to delve into the language-specific construction of the "prototype" javascript. And here is my training code:

var f = function() {}; f.ext = 1; f.prototype.pext = 2; 

When I debug this code now with firebug, I get the following: enter image description here

Where does this endless nesting come from? Let it start from above (OK = unterstood):

f (OK)
- ext (OK)
- prototype (OK)
- pext (OK)
- constructor (I'm stuck at this point)

Whose constructor is this? And why do we have this infinte nesting?

+6
source share
2 answers

This is simply because f === f.prototype.constructor is the same, and Firebug shows them as circular links.

Same as:

 var a = {}, b = a; ab = b; 

Here you will see endless links.

+5
source

When you do it

 Foo = function () { // Do something }; 

then you will have it

 Foo.prototype.constructor == Foo 

as Foo is a constructor for Foo :

 var foo = new Foo(); // <-- Foo is the constructor. 

I recommend this reading: http://beej.us/blog/data/javascript-prototypes-inheritance/

+3
source

All Articles