Skip to content Skip to sidebar Skip to footer

Diffrence Between Assigning Simple Prototype And Using Object.create(object.prototype) In Protypal Inheritence In Javascript?

I am implementing Prototypal inheritence in java script using simple assigning of prototype this is my code - var Rectangle = function(heigth, width) { this.height = height; t

Solution 1:

Because the statement Square.prototype = Rectangle.prototype; actually copies the reference of the Rectangle.prototype and assigns it to Square.prototype. (Primitive types are copied by value and reference types are copied by reference).

So, if you are going to add some properties and methods on Square.prototype it'll modify the original Rectangle.prototype object and which is not what you want.

The Square.prototype = Object.create(Rectangle.prototype) creates a new object whose prototype is the Rectangle.prototype which is correct way.

Post a Comment for "Diffrence Between Assigning Simple Prototype And Using Object.create(object.prototype) In Protypal Inheritence In Javascript?"