Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript Getters and Setters Getters

travis halarewich
travis halarewich
9,165 Points

confusion on the syntax for getters

in the video they use the example get activity() { const today = new Date(); const hour = today.getHours();

if (hour > 8 && hour <= 20) {
  return 'playing';
  } else {
    return 'sleeping';
}

}

my question is to do with the parentheses after activity, if getters are creating properties not methods then why the ()? When we call on this property we don't include them why are they there? If getters can't accept perimeters then why have them?

travis halarewich
travis halarewich
9,165 Points

get activity() { const today = new Date(); const hour = today.getHours(); if (hour > 8 && hour <= 20) { return 'playing'; } else { return 'sleeping'; } }

this is the example I am referring to, it got chopped up a bit when i posted it

1 Answer

Steven Parker
Steven Parker
231,268 Points

Getters are methods, but they are just accessed like attributes. Except for the word "get", they look like any other method definition that takes no argument but returns a value.