Javascript Endless Prototype Chain

I am using the following test code:

function Test() {

 }

 Test.prototype.MyMethod =  {
    a: function() {

    },
    b: function() {

    }
 }

And to run, I just do:

var test = new Test();
console.debug(test);

In the firebug console, I expand the object that was printed, and look inside __proto__:

It contains a seemingly infinite constructor chain -> prototype:

+MyMethod
-constructor
  -prototype
     +MyMethod
     -constructor
        -prototype
           +MyMethod
           -constructor

etc. Did I do something wrong? Why does the prototype chain seem endless?

+3
source share
2 answers

prototypehave a property constructorthat refers to a function that owns the prototype.

This design cycle.

+6
source

The prototype constructor refers to itself, since you "deploy" it, you open the same structure again and again. It is not "infinite."

+3
source

Source: https://habr.com/ru/post/1786507/


All Articles