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

162
Views
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 answers
Answer question

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 Report

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