Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
class Car {
  constructor(make, model, price) {
    this._make = make
    this._model = model
    this._price = price
  }
  // instance methods
  
  get price() {
    return this._price
  }
  
  drive() {
    console.log('calling "this" in an instance method', this)
    console.log(`driving ${this._make} ${this._model}`)
  }
  // static method
  static mostExpensiveCar(cars) {
    console.log('calling "this" in a static method', this)
    return cars.sort((a, b) => b.price - a.price)[0]
  }
}
// Create 3 instances of the Car class
const car1 = new Car("honda", "accord", 20000)
const car2 = new Car("aston martin", "db7", 110000)
const car3 = new Car("bmw", "5 series", 50000)
// call static method mostExpensiveCar() on the Class (Car)
// not the instance
const priceyCar = Car.mostExpensiveCar([car1, car2, car3])
console.log(priceyCar)
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x