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 Setters

Getting "setter called: ${owner}" instead of "setter called: Ashley" in console

class Pet {
  constructor(animal, age, breed, sound) { 
    this.animal = animal;
    this.age = age;
    this.breed = breed;
    this.sound = sound;
  };

  get activity() {
    const today = new Date();
    const hour = today.getHours();

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

get owner() {
  return this._owner;

}

set owner(owner) {
  this._owner = owner;
  console.log("setter called: ${owner}");
}

  speak() {
    console.log(this.sound);
  }
}

const ernie = new Pet("dog", 1, "pug", "yip yip");
const vera = new Pet("dog", 8, "border collie", "woof woof");


ernie.owner = "Ashley";
console.log(ernie.owner);

results in

setter called: ${owner}

Ashley

2 Answers

Steven Parker
Steven Parker
231,268 Points

When using template strings, the string must be enclosed with accents (or "back-ticks"). Other kinds of quote marks will cause the string to be interpreted as an ordinary string literal.

  console.log(`setter called: ${owner}`);  // <- note: accents, not quote marks
Ben Henson
Ben Henson
15,235 Points

Thanks @Steven Parker. I was having the same issue.

Alex Franklin
Alex Franklin
12,403 Points

Me too... Seems like a pretty dang important part of code to leave unexplained in the video....

Thanks Steven!