Skip to content

Instantly share code, notes, and snippets.

@dydx
Created January 26, 2016 16:50
Show Gist options
  • Save dydx/d7fe1da9394277f85fad to your computer and use it in GitHub Desktop.
Save dydx/d7fe1da9394277f85fad to your computer and use it in GitHub Desktop.
'use strict';
class Car {
constructor(year, make, model, convertible) {
this.year = year;
this.make = make;
this.model = model;
// default parameters, much?
if (typeof convertible === 'undefined') {
this.convertible = false;
} else {
this.convertible = convertible;
}
this.speed = 0;
}
accelerate(delta) {
this.speed += Math.abs(delta);
}
decelerate(delta) {
this.speed -= Math.abs(delta);
}
convertibleString () {
return this.convertible ? 'is' : 'is not';
}
toString () {
return `This ${this.year} ${this.make} ${this.model}, which ${this.convertibleString()} a convertible, is travelling at ${this.speed}mph`;
}
}
module.exports = new Car(2016, 'BMW', 'M6');
const car = require('./car.js');
// going nowhere fast
console.log(car.toString());
// not quite topped out, but definite illegal in the US
car.accelerate(200);
console.log(car.toString());
// slightly more reasonable
car.decelerate(120);
console.log(car.toString());
// I'd like to see this shit in reality
car.convertible = true;
console.log(car.toString());
This 2016 BMW M6, which is not a convertible, is travelling at 0mph
This 2016 BMW M6, which is not a convertible, is travelling at 200mph
This 2016 BMW M6, which is not a convertible, is travelling at 80mph
This 2016 BMW M6, which is a convertible, is travelling at 80mph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment