Aquí está mi solución, que se basa en el método de herencia prototípico estándar descrito en la respuesta de Lorenzo Polidori .
Primero, comienzo definiendo estos métodos auxiliares, que hacen que las cosas sean más fáciles de entender y más legibles más adelante:
Function.prototype.setSuperclass = function(target) {
this._superclass = target;
this.prototype = Object.create(this._superclass.prototype);
this.prototype.constructor = this;
};
Function.prototype.getSuperclass = function(target) {
return this._superclass;
};
Function.prototype.callSuper = function(target, methodName, args) {
if (arguments.length < 3) {
return this.callSuperConstructor(arguments[0], arguments[1]);
}
if (args === undefined || args === null) args = [];
var superclass = this.getSuperclass();
if (superclass === undefined) throw new TypeError("A superclass for " + this + " could not be found.");
var method = superclass.prototype[methodName];
if (typeof method != "function") throw new TypeError("TypeError: Object " + superclass.prototype + " has no method '" + methodName + "'");
return method.apply(target, args);
};
Function.prototype.callSuperConstructor = function(target, args) {
if (args === undefined || args === null) args = [];
var superclass = this.getSuperclass();
if (superclass === undefined) throw new TypeError("A superclass for " + this + " could not be found.");
return superclass.apply(target, args);
};
Ahora, no solo puede establecer la superclase de una clase con SubClass.setSuperclass(ParentClass), sino que también puede llamar a métodos anulados con SubClass.callSuper(this, 'functionName', [argument1, argument2...]):
function Transform() {
this.type = "2d";
}
Transform.prototype.toString = function() {
return "Transform";
}
function Translation(x, y) {
Translation.callSuper(this, arguments);
this.x = x;
this.y = y;
}
Translation.setSuperclass(Transform);
Translation.prototype.toString = function() {
return Translation.callSuper(this, 'toString', arguments) + this.type + " Translation " + this.x + ":" + this.y;
}
function Rotation(angle) {
Rotation.callSuper(this, arguments);
this.angle = angle;
}
Rotation.setSuperclass(Transform);
Rotation.prototype.toString = function() {
return Rotation.callSuper(this, 'toString', arguments) + this.type + " Rotation " + this.angle;
}
translation = new Translation(10, 15);
console.log(translation instanceof Transform);
console.log(translation instanceof Translation);
console.log(translation instanceof Rotation);
console.log(translation.toString())
Es cierto que incluso con las funciones de ayuda, la sintaxis aquí es bastante incómoda. Afortunadamente, sin embargo, en ECMAScript 6 se ha agregado algo de azúcar sintáctico ( clases mínimas máximas ) para hacer las cosas mucho más bonitas. P.ej:
class Transform {
constructor() {
this.type = "2d";
}
toString() {
return "Transform";
}
}
class Translation extends Transform {
constructor(x, y) {
super();
this.x = x;
this.y = y;
}
toString() {
return super(...arguments) + this.type + " Translation " + this.x + ":" + this.y;
}
}
class Rotation extends Transform {
constructor(angle) {
super(...arguments);
this.angle = angle;
}
toString() {
return super(...arguments) + this.type + " Rotation " + this.angle;
}
}
translation = new Translation(10, 15);
console.log(translation instanceof Transform);
console.log(translation instanceof Translation);
console.log(translation instanceof Rotation);
console.log(translation.toString())
Tenga en cuenta que ECMAScript 6 todavía se encuentra en la etapa de borrador en este momento, y que yo sepa no está implementado en ningún navegador web importante. Sin embargo, si lo desea, puede usar algo como el compilador de Traceur para compilar ECMAScript 6en el ECMAScript 5JavaScript simple y antiguo . Puede ver el ejemplo anterior compilado con Traceur aquí .