Skip to content

Instantly share code, notes, and snippets.

@thatPamIAm
Created December 5, 2018 15:57
Show Gist options
  • Save thatPamIAm/23f7b57a7573838f6d9cad9826abe8be to your computer and use it in GitHub Desktop.
Save thatPamIAm/23f7b57a7573838f6d9cad9826abe8be to your computer and use it in GitHub Desktop.

Given these two classes, what properties and/or methods would a parent class of Pet have? What would need to modified/removed/added on our classes of Dog and Cat for them to inherit from Pet?

class Dog {
  constructor(name, breed, tricksArray) {
    this.name = name;
    this.breed = breed;
    this.tricks = tricksArray;
  }

  speak() {
    console.log('Noise');
  }

  doTrick(trick) {
    if ( this.tricks.includes(trick) ) {
      return `${this.name} does a trick: ${trick}!`
    } else {
      return `${this.name} doesn't know how to ${trick}!`
    }
  }
}

// Cat.js

class Cat {
  constructor(name, breed, faveTreat) {
    this.name = name;
    this.breed = breed;
    this.faveTreat = faveTreat;
  }

  speak() {
    console.log('Noise');
  }

  feedTreat(treat) {
    if ( this.faveTreat == treat ) {
      return `${this.name} eats a treat: ${treat}`
    } else {
      return `${this.name} casually sniffs the ${treat} and then ignores it.`
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment