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

184
Views
Need explanation for 'under the hood' of get and set in JavaScript

Given the code below even though the name of the parameter is on the beginning firstName, after setting it with setter and looking at the properties of an object it becomes _firstName. I am coming from a Java development background and this seems unlogical to me. Can somebody explain, please?

class Person {
constructor(firstName, DOB, sport) {
        this.firstName = firstName;
        this.DOB = DOB;
        this.sport = sport;
    }   
get firstName() {  
        return this._firstName;
    }
set firstName(firstName) {
        this._firstName = firstName;   
    }
}

const person1 = new Person('Marko', '2002', 'tennis');`

Object after person1.firstName = 'Luka';

(Person {_firstName: 'Luka', DOB: '1256', _sport: 'tennis'}
DOB: "1256"
_firstName: "Luka"
_sport: "tennis"
DO: undefined
firstName: (...)
sport: (...)
[[Prototype]]: Object)

I tried person1.firstName = 'Luka'; and was expecting object field to stay firstName but it became _firstName (with underline). I see that I am making this change in this line this._firstName = firstName; but I do not understand how can variable name be overriden

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

this.firstName is a method setting the property this._firstName.

Calling the setter method this.firstName = "Marko" works similar to calling a standard method like this.firstName("Marko"). Since you're using the method in your constructor it "looks like" you're setting the property this.firstName when you're actually using the method this.firstName to set the property this._firstName.

Modifying this.firstName method helps understand this:

class Person {
    constructor(firstName, DOB, sport) {
        this.firstName = firstName;
        this.DOB = DOB;
        this.sport = sport;
    }   
get firstName() {  
        return this._firstName;
    }
set firstName(firstName) {
        //this will output on the console whenever the method is being used
        console.log("setter method used");
        this._firstName = firstName;   
    }
}
const person1 = new Person('Marko', '2002', 'tennis');

You should see on the console that the setter method was used once. Now try changing the first line of your constructor to this._firstName = firstName;

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!