Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda