theonlychase
9/28/2018 - 1:49 AM

Classes

// Simple Class
// - The only method that is absolutely required is what is called a `constructor`
// - Whenever you have a method inside of a class, do not put a comma between them
// - Static Method - Can only call it on the constructor itself created. It will not work when calling it on an instance

class Animal {
  constructor(species, color) {
    this.species = species,
    this.color = color
  }
  static info() {
    console.log('Here is the info');
  }
  get description() {
    console.log(`This is a description of the ${this.species}`);
  }
  set newColor(value) {
    this.color = value;
  }
  get newColor() {
    return this.color;
  }
}
Animal.info(); // calls static method
const bear = new Animal('bear', 'brown');
console.log(bear.species, bear.color); // bear brown
bear.newColor = 'red'; // how you set the new age. //red

// --EXTENDING CLASSES AND USING SUPER-- //
// In the example below, we would need to create an animal before we create a Dog, Which is an extension of Animal.
// We do this by calling `super()`
// It means just call the thing that you are extending first.
class Animal {
  constructor(species, color) {
    this.species = species,
    this.color = color
  }
  static info() {
    console.log('Here is the info');
  }
  get description() {
    console.log(`This is a description of the ${this.species}`);
  }
  set newColor(value) {
    this.color = value;
  }
  get newColor() {
    return this.color;
  }
}

class Dog extends Animal {
  constructor(species, name) {
    super(species);
    this.name = name;
  }
  bark() {
    console.log(`${this.name} likes to bark`);
  }
}

let dog = new Dog('husky','Link');
dog.newColor = 'red';