Classy goal writing

For the last several posts, I’ve been writing goals in JavaScript like this:

const myGoal = {
  action: 'Write',
  quantity: 500,
  measurement: 'words'
}

const myOtherGoal = {
  action: 'Eat',
  quantity: 20,
  measurement: 'marshmallows'
}

It starts to get tedious having to remember that I need an action, quantity and measurement whenever I want to write a goal. Instead of starting with a bare-bones object each time, I can create a goal prototype by defining a class.

class Goal {
  constructor(action, quantity, measurement) {
    this.action = action;
    this.quantity = quantity;
    this.measurement = measurement; 
  }
}

Now whenever I want to create a new goal, I use the Goal class to create or construct it.

const myGoal = new Goal('Write', 500, 'words');
const myOtherGoal = new Goal('Eat', 20, 'marshmallows');


Discover more from AaronTweeton.com

Subscribe to get the latest posts sent to your email.