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

163
Views
¿Es el constructor un método estático de una clase?

Hola soy nuevo en JavaScript. Me pregunto si el constructor de clase es un método estático de clase especial. Tengo un ejemplo de este tipo:

 class warrior { constructor(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } get fullName() { return `${this.firstName} ${this.lastName}`; } sayHello() { return `Hello my name is ${this.fullName}`; } static createWarrior(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age; return this; } } let warrior1 = new warrior("Ashley", "Yuna", 25); warrior1.sayHello(); let warrior2 = warrior.createWarrior("Kris", "Snow", 23); warrior2.sayHello();

Si el constructor de clase es un tipo de método estático, ¿cómo puedo modificar el método estático createWarrior para poder devolver la instancia de guerrero?

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

0

Hipotéticamente podría lograr esto utilizando el método static para devolver una new instancia de la clase en sí:

 class warrior { constructor(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } get fullName() { return `${this.firstName} ${this.lastName}`; } sayHello() { return `Hello my name is ${this.fullName}`; } static createWarrior(firstName, lastName, age) { return new this(firstName, lastName, age); } } let warrior1 = new warrior("Ashley", "Yuna", 25); console.log(warrior1.sayHello()); let warrior2 = warrior.createWarrior("Kris", "Snow", 23); console.log(warrior2.sayHello());

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!