1 min readJul 14, 2020
Yeah, that works. Good thinking. But you also need to preserve the Child prototype. Object.assign
was introduced in ES6, so you need to also take care of that.
function createObject( prototype ) {
function Wrapper() {}
Wrapper.prototype = prototype;
return new Wrapper();
}function extend(Child, Parent) {
var childPrototype = createObject( Parent.prototype );childPrototype.constructor = Child;
Child.prototype = Object.assign( childPrototype, Child.prototype );
}function Animal() {}
Animal.prototype.category = 'ANIMAL';function Dog() {}
Dog.prototype.kind = 'DOG';extend(Dog, Animal);console.log((new Dog()).category); // ANIMAL
console.log((new Dog()).kind); // DOG
BTW, you can also use this var Wrapper = new Function();
but this is really unreliable and unnecessary.