Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 920 Bytes

classes.md

File metadata and controls

70 lines (53 loc) · 920 Bytes

Classes

Alternative to constructors

class Car {
  
}

let car = new Car()
console.log(car) // Car {}

Constructors

Instantiate class with attribute values

class Car {
   constructor(id, color='black'){
       this.id = id
       this.color = color
   }
}

let car = new Car(4)
console.log(car)

Methods

class Car {
   constructor(id, color='black'){
       this.id = id
       this.color = color
   }
  
  //no need for function keyword for class methods
  identify(){
    return `Car ${this.id}`
  }
}

let car = new Car(4)
console.log(car.identify())

Inheritance

class Vehicle{
  constructor(){
    this.type = 'car'
  }
  
  start(){
    return `Starting ${this.type}`
  }
}

class Car extends Vehicle{
  start(){
    return 'in car start ' + super.start()
  }
}

let car = new Car()
car.start() // 'in car start Starting car'