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

167
Visualizações
What exacly happens when a class constructor calls a setter

Hi at this code example i can't fully understand when the setter fullname method gets called from the constructor why the constructor does not create also a property "fullname" but it just invokes the setter and moves to the next line.

Can someone explain to me what happens in detail it will be much appreciated.

class Person {
  constructor(fullname, birthYear) {
    this.fullname = fullname;
    this.birthYear = birthYear;
  }          
  set fullname(name) {
    if (name.includes(' ')) this._fullname = name;
    else alert(`${name} is not a full name`);
  }
 
  get fullname() {
    return this._fullname;
  }
}
 
const personObj = new Person('FirstName Lastname', 1999);
console.log(personObj);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The new keyword creates an object with the prototype of Person, then calls the constructor. So by the time the constructor is called, it already has that object, and that object has a pair of setters and getters for this.fullname. If you attempt to assign to this.fullname, the standard behavior for setters and getters occurs, and the setter will be called. The behavior doesn't get modified due to being inside a constructor function.

about 4 years ago · Juan Pablo Isaza Relatório

0

The constructor does not create a new property named fullname since that property already exists, defined by the getter and setter. However it is not a data property but an accessor property, so it holds no value of it's own.

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