Class with Getters & Setters

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', 'Woof!');
const vera =     new Pet('dog', 8, 'Border Collie', 'Woof!');
const scofield = new Pet('dog', 6, 'Doberman', 'Woof!');
const edel =     new Pet('dog', 7, 'German Shorthaired Pointer', 'Woof!');

Note: the constructor method is not required inside of a class.
 

Getters

Getter methods are used to dynamically create and retrieve a property method – a property with dynamic values. When a getter method get is called, a property value is computed and returned. This value returned from a getter method is stored nowhere. Properties with dynamic values should not be set in the constructor method.

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

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

If you were to output the ernie object to the console you would not see the new activity property.
A value for the activity property is computed and returned from the getter method when we access it, but never actually attached to the object.

console.log(ernie.activity); // 'sleeping'
console.log(ernie); // Pet { animal: 'dog', age: 1, breed: 'Pug', sound: 'Woof!' } 

 

Setters

Setter methods are used when you need to add logic when updating a property value.

  1. A setter method is used when you need to add logic when updating a property value
  2. A setter method always receives exactly one parameter, the parameter is the value of the property that we’d like to set.
  3. A setter can either update an existing property with that value, or store the value to a new property (i.e. this._owner).
  4. The name of a setter method cannot be equal to the name of an existing property.
    get owner () { return this._owner; }

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

 

Naming a Setters

We can’t do this this.owner because of this set owner() { ... }. So, the convention dictates to use the name of the setter function but with an underscore behind it this._owner.  

Setting the Property Value
ernie.owner = 'Ashley';

Just like you’re setting a property value using dot notation, but instead, the value is being set to a backing property this._owner. That’s why when we try to retrieve the value of owner this way we get undefined:

console.log(ernie.owner); // undefined

To solve this, we’ve got to create a getter method called owner that returns the value of that backing property right before the set method (i.e. get owner ()).
 

Class Properties Can Be Objects

Making the owner property an object.

class Owner {
    constructor(name, address) {
        this.name = name;
        this.address = address;
    }

    get phone() { return this._phone }
    set phone(phone) {
        this._phone = phone;
    }
}
ernie.owner = new Owner('Ashley', '123 Main Street');
ernie.owner.phone = '555123494';