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

164
Vistas
Is constructor a static method of a class?

Hi I am new in JavaScript. I am wondering if the class constructor is a special class static method. I have such an example:

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

If class constructor is a kind of static method, how can I modify createWarrior static method to be able to return warrior instance?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could hypothetically accomplish this by using the static method to return a new instance of the class itself:

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 Denunciar
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