0

I am learning subclassing in javascript and came across the following example. My question is what is the reason for writing Derived.prototype = Object.create(Base.prototype); instead of Derived.prototype = Base.prototype;.

function Derived()
{
    //set some properties 
}
Derived.prototype = Object.create(Base.prototype); //what will be the difference if we wrote just Base.prototype without Object.create on the right hand side

As you can see I do not understand why we don't just use Derived.prototype = Base.prototype;. I mean are there any benefits of passing Base.prototype in Object.create() instead of using it without any Object.create(). Maybe there is a reason why the Derived.prototype = Object.create(Base.prototype) is preferred over Derived.prototype = Base.prototype; which I don't know

Alex
  • 318
  • 1
  • 14
  • 1
    If you add methods (or properties in general) to `Derived.prototype` without initially using `Object.create()`, then you'll be modifying the object `Base.prototype`, so anything else that also "extends" `Base.prototype` will have now have these methods, not just `Derived.prototype` – Nick Parsons Jan 15 '23 at 11:59

0 Answers0