0

Hi I created a class as below:

function Shape(){
     this.x = 0;
     this.y = 0;
}

Now in the Chrome console when I type Shape.prototype it has constructor property which on expanding has a prototype and __ proto __(dunder proto points to the Object Base and the chain ends here, I get this point.

But the prototype property upon expanding contains constructor property and this chain goes on and on (constructor to prototype and prototype to constructor) . Why is this happening?

Is it because that .prototype and .constructor keep pointing at each other or is there something else going on over here? can please someone explain.

Thanks in advance

pi2018
  • 347
  • 2
  • 11
  • 1
    Yes, they are two objects pointing at each other. There's no infinite amount of objects, just a circular reference. – Bergi Aug 27 '18 at 13:36
  • Thanks for pointing to the question. I tried searching for a similar question but couldn't find it. Another quick query why does the constructor's dunder proto point to a f:{} (shape object) wheareas the prototype's dunder proto point to base Object. – pi2018 Aug 27 '18 at 13:53
  • Because the constructor (function) inherits from `Function.prototype` – Bergi Aug 27 '18 at 13:55
  • Thank you, this makes complete sense now. Because functions are first class objects and the function prototype inherits from the Object base again. – pi2018 Aug 27 '18 at 13:57

0 Answers0