0

I was going through the concept of inheritance ,Now the situation is that let us have and empty constructor function

function Constructor(/* i am an empty function  */ );

Now we are going to add a property to Object.prototype and make an instance of Constructor function

Object.prototype.game = "hello i am a game";
 let a =  new Constructor();
 a.game();

Now we Know the output is Going to be "hello i am a game".Therefore var a inherits from Object and therefore Constructor function inherits from Object therefore

Constructor.__proto__ === Object.prototype

Must be true than why is it coming out to be false

HARSH BAJPAI
  • 45
  • 12
  • 2
    `__proto__` and `prototype` both are objects. So you cannot use `===` on them – Rajesh Dec 26 '17 at 11:04
  • Your example code does not work (`Uncaught SyntaxError: Unexpected token ;` and `Uncaught TypeError: a.game is not a function`), so what exactly are you even asking about? – Bergi Dec 26 '17 at 11:33
  • "inherits from" is not the same as "directly inherits from". So instead of testing `Object.getPrototypeOf(a) == b` (btw, do not use the deprecated `__proto__`!), you would need to do `b.isPrototypeOf(a)` which traverses the whole chain – Bergi Dec 26 '17 at 11:35

1 Answers1

2

Constructor.proto === Object.prototype

No that should not be true. What should be true is as follows:

a.__proto__ === Constructor.prototype

However (in this case)

Constructor.prototype.__proto__ === Object.prototype

should be true.


First one directly follows from prototype definition. __proto__ property of an object created from a constructor function is equal to prototype of that function.

Second one follows from fact that Constructor.prototype is also an object - and was created using Object constructor function (hence similar to first case its __proto__ must be equal to prototype of constructor function, which is Object.prototype).

PS. But to directly answer your question to what Constructor.__proto__ equals, it is Function.prototype (reasoning could go like: this is because Function object was the one who created your Constructor function).

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90