JavaScript Object-Oriented JavaScript Getters and Setters Getters

Example with Conditional (ternary) operator

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()

    return hour > 8 && hour <= 20 ? "playing" : "sleeping"

  }

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

}

const vera = new Pet ("Dog", 7, "border collie", "Woof!")

console.log(vera.activity)
Steven Parker
Steven Parker
182,603 Points

This code seems to function correctly, did you have a question about it?

1 Answer

He Steven, thank you. The idea was only to show an example with ternary. Best regards