Skip to content

Instantly share code, notes, and snippets.

@JoeKarlsson
Last active September 14, 2016 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoeKarlsson/602a5858ef78582d4ca5 to your computer and use it in GitHub Desktop.
Save JoeKarlsson/602a5858ef78582d4ca5 to your computer and use it in GitHub Desktop.
ES6 OOP Example
'use strict'
const Rocket = require('../lib/Rocket');
const Missile = require('../lib/Missile')
var apollo13 = new Rocket('Apollo', 'Moon', 1993);
var mach10 = new Missile(1000000, 2016);
console.log(mach10.year);
'use strict'
const Rocket = require('./Rocket.js');
module.exports = class Missle extends Rocket {
constructor( speed, year ) {
super('Mach 10', 'North Korea', year)
this._speed = speed;
}
get speed() {
return this._speed;
}
}
'use strict'
var Vehicle = require('./Vehicle.js');
module.exports = class Rocket extends Vehicle {
constructor( model, destination, year ) {
super( year, 'Red' );
this._model = model;
this._destination = destination;
this.fuel = 100;
this._launchCount = 0;
}
get destination() {
return this._destination;
}
set destination( place ) {
if (typeof place !== 'string') {
throw new TypeError('Rocket.destination must be a string')
}
this._destination = place;
}
launch() {
this._launchCount++;
console.log('TO THE MOON');
}
}
'use strict'
module.exports = class Vehicle {
constructor ( year, color ) {
this._year = year;
this._color = color;
}
get year() {
return this._year;
}
set year(year) {
if (typeof year !== 'number') {
throw new TypeError('Vehicle.year must be a number')
}
this._year = year;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment