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