Meal Maker: What's the point of the getter/setter methods?

I just finished the Meal Maker project in the JavaScript path, and all went fine but it totally confused for me the purpose of getter/setter methods in objects. The previous exercise had good examples of using these to either manipulate data inside the object (like combining a firstName property and lastName property into a “fullName” getter method that you can call) or verifying data types before changing the value of a property with a setter method. However, in this exercise, the getter/setter methods do nothing. I deleted them at the end of the exercise and my code still ran fine. I watched the project walkthrough video, and all the guy said was “it’s best practice to set-up getter/setter methods to access your object’s properties” but doesn’t explain why.

Are these used so frequently in coding that I should just get in the habit of using them for any and all objects? Or am I missing something about what purpose they’re serving in this case? Any help would be greatly appreciated!

EDIT: Here’s the link to the project - https://www.codecademy.com/paths/create-video-games-with-phaser/tracks/game-dev-learn-javascript-objects/modules/game-dev-learn-javascript-objects/projects/meal-maker

It’s difficult for any learner with no legacy wherewithal to grasp the JS getter/setter mentality. Understanding how it is implemented in JS is further impeded by that lack of background context.

Getters and setters are not a JavaScript paradigm, but a programming one. They are ubiquitous across a great many languages. To be able to answer you questions we best defer to why they were incorporated into the language? They turned this into a JerryScript language, just rigged to pretend to work like other languages. The burden became the designer’s to come up with implementations that could be further incorporated in later iterations of ES.

The goal from here would be to seek out well crafted employment of the ES get/set properties, and in particular how to make their backing variables truly private. It’s a bit of a rabbit hole, so dress warm and bring f/w.

Imagine you’re working on a large coding project with many other people. You might want these other people to only interact with a class you write in certain ways. So you write getters and setters, and as long as people just use these, you know how they’ll interact with your class.

That’s basically what getters and setters are for. Someone could still start messing with your class, but getters and setters are a convention of “This is the only way we’re going to mess with this bit of code” and then everybody knows what’s going to happen.

That’s why it’s good to get into the habit of using them correctly before you need to use them as part of a large project.

2 Likes

This is helpful - thank you!

1 Like

Elegant reply. Thank you.

It would seem that an ‘add_’ or ‘remove_’ setter would be useful based on this methodology and exercise. That would lock up some built-in functions. For instance, using only the setters and getters there is no way to write a statement Object.assign(this.appetizers, dish); One would have to code forcefully into the _courses object hopefully causing a red flag to the team member.

In this case I used my setters to add items to _courses object rather than directly accessing them. This allows data validation and other things as well. Looks like this:

. . .
  set add_appetizers(dish) {
    this._courses.appetizers.push(dish);
  },
  set add_mains(dish) {
    this._courses.mains.push(dish);
  },
  set add_desserts(dish) {
    this._courses.desserts.push(dish);
  },
. . .
addDishToCourse(courseName, dishName, price) {
. . .
 const dish = {
      name: dishName,
      price
    }
    this['add_' + courseName] = dish;
  },
. . .

*** I edit because there is no preview in this forum.

1 Like