Search code examples
javascriptobjectinheritanceprototypal

Understanding Prototypal Inheritance


var Object1 = {};
var Object2 = new Object();
var Object3 = Object.create({});

When i check whether the prototype is equal to Object.prototype:

The first two return true while the third one returns false.

Why is this happening?

Object.getPrototypeOf(Object1)===Object.prototype //true
Object.getPrototypeOf(Object2)===Object.prototype //true
Object.getPrototypeOf(Object3)===Object.prototype //false

Solution

  • Simply because if you take a look at the Object.create() in the documentation, you will that this method:

    creates a new object with the specified prototype object and properties.

    And if you call it with :

    Object.create({})
    

    You are not passing a prototype but an empty object with no properties.

    So as stated in comments you need to call it like this:

    Object.create(Object.prototype)