I am reading kangax's blog on How ECMAScript 5 still does not allow to subclass an array. Here he is using a different approach of subclassing than the normal prototypal construct
BaseClass.prototype = new Superclass();
What he is doing is this :
function clone(obj) {
function F() { }
F.prototype = obj;
return new F();
}
and then set-up inheritance like this:
function Child() { }
Child.prototype = clone(Parent.prototype);
Can someone explain this two part approach of inheritance and what benefits it gives over the simple one liner approach above?
Edit: I understand from the comments that there is now a standard Object.create()
that basically solves the same purpose as clone()
method but how does this implementation of clone()
work ?
Object.create
for inheritance – Felix Kling 36 mins agonew Superclass()
may trigger undesired side effects. Theclone
gives you an object that inherits from the parent, but without having to invoke the constructor function. And FYI, there's now a standard method in JavaScript that does this. It'sObject.create()
– Crazy Train 36 mins agoclone
function in your example has the same purpose asObject.create
, that's why the answers to the linked question will (hopefully) help you as well. – Felix Kling 36 mins agonew Superclass()
is invoked? – Geek 24 mins agoclone()
. – Geek 18 mins ago