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

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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