Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
How to emulate super to override a parent method outside a class?

To test my understanding of how classes work and how they can be emulated, I've done the following:

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);

However, in trying to override the toString() method (while also retrieving the parent value), I'm running into a recursion error.

It seems the following works if I alias a parent method, but I'm wondering if it's possible to do it without changing one of the method names?

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);

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda