Having hard time learning Getter and Setter concepts

Hi Campers,

I hope you all are doing great. I am so much thankful to freeCodeCamp for giving such a great learning material and that too for free. I’ve been enjoying my learning process and man, it feels so damn good to complete those challenges.

However, I think that ES6 immediately after Basic Javascript is a huge jump because I found that there are many things that are not explained and they expect us to learn from elsewhere and apply the concept. I was moving at the pace of 15-20 challenges per day in other secitons but when I came to ES6, the experience was scary as I could only process 4-5 challenges a day because it needed thorough research on those topics. At first, I thought I’ll just skip that section but later changed my mind.

So far, I’ve successfully grasped all the concepts except one thing and that’s why I’m here, would be great if any of you could explain it well.

I don’t understand the need of getters and setters in class constructor. I researched and found that it’s about giving indirect access to user rather than direct access but I am not getting it well, because for example:

var obj = {
name: “jslearner”,
interest: “javascript”
};

I can simply edit those properties with “dot” notation or “bracket” notation. Now if I set a getter and setter on the object, I will now have two ways to edit the object and the end user can manipulate both of them. Nowadays, text-editor shows which properties are owned by an object, so if I ask my user to edit obj.name with obj.setname, he can do it with either and there seems no point. if he just want to get the value, he will just write obj.name instead of obj.getname.

I can only think of one use-case and that is when we want to calculate something using object properties and return the appropriate value.

In short, it seems to me that getter and setter just introduce “one more way” to access the object while the object is still mutable and accessible by dot and bracket notation.

1 Like

Yes, this is correct, that’s the use case, and because JS objects are so easy to manipulate the usefulness of getters and setters is not high compared to a classical OO language (like Java/C#/Ruby/etc).

Yeeess, you are correct but that’s not what you would do, the point is to provide an interface for an end user. They may well be able to edit the underlying properties directly, but the getters and setters are there to allow you to provide an interface that may do some computation on the underlying (private) properties of the object itself. Also they aren’t terribly useful with plain objects

class Person = {
  constructor(name, interest) {
    this._name = name;
    this._interest = interest;
  }

  get userInterest() {
    return `${this._name} is interested in ${this._interest}.`;
  }

  set userInterest(interest) {
    this._interest = interest;
  }
}

used like

> const dan = new Person('Dan', 'proogramng');
> dan.userInterest
'Dan is interested in proogramng.'
> dan.userInterest = 'programming'
> dan.userInterest
'Dan is interested in programming.'

They have some use in JS but in it is likely that you won’t use them much [if at all]. Note they aren’t an ES6 feature at all, they’ve been in the language since ~2011 (and in certain browsers well before that).

1 Like

@DanCouper You’re the man. Thanks for your well-explained answer, especially by affirming my understanding and correcting it.

1 Like

:slight_smile: I found them extremely wierd when I started - I think if I had had exposure to Java or a similar language before I learned JS then they would have seemed a normal thing. I think I’ve used a getter once in production code the entire time I’ve worked – they don’t seem to fit very well into how JS is written most of the time. They do seem a bit pointless, but they have some usecases. It’s sometimes really important to have a method that always computes the current properties on an object - if those properties are being updated quite a bit, and you need the latest ones, getters in particular can be useful.

1 Like

I was really confused when I first came to know about getter and setter. Coming with a little bit experience of C#, I had that thing in my mind that it’s just a way to access and set, but when there are better ways to access like dot and bracket notation in JS, better not complicate things more for the sake of team coding.

But knowledge is power, just like you said that sometimes we can really need getter or setter, it’s a good option.

1 Like