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

179
Views
¿Cómo llamar al método de función del constructor secundario?

Cuando trato de llamar al método (método obtener detalles () del maestro) de la función de constructor secundario, se llama al método constructor principal. ¿No se supone que el método secundario sombrea el método principal? ¿Cómo llamar al método del constructor hijo getDetails()?

 let Person = function() { }; Person.prototype.personName = "Smith"; Person.prototype.age = 37; Person.prototype.getDetails = function() { return `Person Name: ${this.personName}. Age is ${this.age}`; }; let Teacher = function() { }; Teacher.prototype.mainSubject = "Physics"; Teacher.prototype.getDetails = function() { return `Main subject is ${this.mainSubject}`; }; Teacher.prototype = Object.create(Person.prototype); // inheritance let teacher1 = new Teacher(); console.log(teacher1.getDetails());

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Asigna un nuevo objeto a Teacher.prototype , por lo tanto, pierde todo lo que le asignó de antemano. Debes crear el objeto antes que todo lo demás.

 let Person = function() { }; Person.prototype.personName = "Smith"; Person.prototype.age = 37; Person.prototype.getDetails = function() { return `Person Name: ${this.personName}. Age is ${this.age}`; }; let Teacher = function() { }; Teacher.prototype = Object.create(Person.prototype); // inheritance Teacher.prototype.mainSubject = "Physics"; Teacher.prototype.getDetails = function() { return `Main subject is ${this.mainSubject}`; }; let teacher1 = new Teacher(); console.log(teacher1.getDetails());

Pero hoy en día también podrías usar clases en lugar de funciones.

 class Person { personName = "Smith"; age = 37; getDetails() { return `Person Name: ${this.personName}. Age is ${this.age}`; } } class Teacher extends Person { mainSubject = "Physics"; getDetails() { return `Main subject is ${this.mainSubject}`; } } let teacher1 = new Teacher(); console.log(teacher1.getDetails());

about 4 years ago · Juan Pablo Isaza Report
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!