I dont understand the get and set function in es6 javascript

Like i dont knoe how it workds, why it works and what the advantages of it are. i was doing the es6 cource and am new to coding.i just dont understand it at all.

I was doing one of the es6 challenges for javascript and there it introduces the get and set function and one of them returns somthing and there is a .this and it doesnt explain it at all and can someone please help me?

Please do not create multiple topics for the same question. I have merged your forum topics.

1 Like

there wasnt enough info in the last one so i deleted it and made this one. sorry i guess

No worries. In the future, you can always update your topics by editing the original post or just replying to yourself.

Let’s just focus on the getter first. From the challenge:

class Book {
  constructor(author) {
    this._author = author;
  // getter
  get writer() {
    return this._author;

const novel = new Book('anonymous');

What specifically don’t you understand about the above code?

dont stress too much on the ES6 section, try to get as much as you can out of it and put it aside. Make mental notes to get back to it later whenever you encounter any of its topics. Lot of the content there comes in advance, compared to other stuff and comes in use only much later.

A class describes a type of thing. It defines what data that thing keeps track of and it defines what that thing can do.
Dogs all have a name, a breed, and a weight. Dogs can bark and they can eat.

An object, or an “instance” of the class is an individual thing. It associates specific data to the correct thing.
This dog’s name is Bandit. He is a schnauzer mix. He weighs 8lbs.

When we are using things in our code, we very often need to know or update its data.
We need to get my dog’s name to print it on the “Best dog of the month” image. We need to set my dog’s weight to something new when my dad feeds him cheese.
“Getters” and “setters” are the name for methods that allow us to interact with data. Needing this kind of data access is so overwhelmingly common that we have a term for it and in some languages there are even shortcuts to write them more easily.

1 Like