Para probar mi comprensión de cómo funcionan las clases y cómo se pueden emular, he hecho lo siguiente:
function Electronic(category) { this.category = category; this.state = 0; } Electronic.prototype.toString = function() { return `The ${this.category} is turned ${this.state? "on" : "off"}.`; } function Computer(brand) { Electronic.call(this, 'Computer'); this.brand = brand; } Computer.prototype = Electronic.prototype; Computer.prototype.constructor = Computer; Computer.prototype.toString = function() { // how to get the super.toString() method here? return `${Electronic.prototype.toString()} The brand is ${this.brand}.`; } c = new Computer('Dell'); console.log('' + c); Sin embargo, al tratar de anular el método toString() (al mismo tiempo que recupero el valor principal), me encuentro con un error de recurrencia.
Parece que lo siguiente funciona si hago un alias de un método principal, pero me pregunto si es posible hacerlo sin cambiar uno de los nombres de los métodos.
function Electronic(category) { this.category = category; this.state = 0; } Electronic.prototype._toString = function() { return `The ${this.category} is turned ${this.state? "on" : "off"}.`; } function Computer(brand) { Electronic.call(this, 'Computer'); this.brand = brand; } Computer.prototype = Electronic.prototype; Computer.prototype.constructor = Computer; Computer.prototype.toString = function() { return `${Electronic.prototype._toString.call(this)} The brand is ${this.brand}.`; } c = new Computer('Dell'); console.log('' + c);