Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
¿Cómo emular super para anular un método principal fuera de una clase?

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!