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

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

confused

I am completely confused after this video. I have several questions.

1 - So Pet is an object as well right, which was created by using Class and Constructor Method and it further makes instances of Pet Object ( As she said, the getter method will not be attached to it (Instance Object), but still can be accessed , that means it is connected to the Pet Object, right?) Because whenever I try to log ernie it always shows as

Pet {animal: 'dog', age: 1 ............}

But if I make an object with Literals it just prints out the properties in {} braces.

2 -

class Pet{
  constructor(animal, age, breed, speak){
    this.animal = animal,
    this.age = age,
    this.breed = breed,
    this.speak = speak
  }
  speak(){
    console.log(this.speak);
}
  activity(){
   const today = new Date();
   const hours = today.getHours();
    if(hours > 8 && hours <= 20){
      return `Playing`;
} else {
     return `Sleeping`;
}
}
}
const ernie = new Pet('dog', 1, 'pug', 'Woof Woof');
console.log(ernie.activity());
console.log(ernie);

In the output it still does not show the activity() Method & speak() attached to ernie?, It is just showing Properties, even if I do this without Getter Method

3 - If we can do everything with normal method too, then why do we need getter method any specific difference, coz I am getting the same answers ?

2 Answers

I think it's really important to understand that, as Ms. Boucher explains at the start of the course, class is just syntactic sugar for prototypes.

Consider the following:

const greeting = "Hello";
greeting.toLowerCase(); // "hello"

You can call .toLowerCase() on greeting, but if you log greeting in the console it's just a string. The 'method' lives on the global String object's prototype, hence why MDN refers to it as String.prototype.toLowerCase(), and any string automatically shares its prototype and can access its methods.

Go back to your code above and write at the bottom:

console.log(Pet.prototype);

Voila! There are the methods. They do not belong on ernie, but ernie can access them because it shares a prototype with the Pet object. That's all a class instantiation does in JS.

For your 3rd question, you may wish to see my answer to another user.

Good luck!

Too many questions.

  1. yes.
  2. yes.
  3. don't know :)