Created
January 26, 2016 16:50
-
-
Save dydx/d7fe1da9394277f85fad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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