According to the official demo The Object.create() method creates a new object with the specified prototype object and properties.
With the following code, why the prototype not equal??
function Shape() {}
function Rectangle() {}
Rectangle.prototype = Object.create(Shape.prototype);
console.log(`${Shape.prototype === Rectangle.prototype}`) // false
Why Shape.prototype === Rectangle.prototype
is false
From MDN:
The Object.create() method creates a new object with the specified prototype object and properties.
Rectangle.prototype
is an object whose prototype is Shape.prototype
. That is, Rectangle.prototype != Shape.prototype
.
In the other hand:
function Shape() {}
function Rectangle() {}
Rectangle.prototype = Object.create(Shape.prototype)
// true
console.log(Object.getPrototypeOf(Rectangle.prototype) == Shape.prototype)
In addition, you may use instanceof
to verify that a given object is an instance of some prototype:
function Shape() {}
function Rectangle() {}
Rectangle.prototype = Object.create(Shape.prototype)
console.log(Rectangle.prototype instanceof Shape)