Last active
September 14, 2016 02:23
-
-
Save JoeKarlsson/602a5858ef78582d4ca5 to your computer and use it in GitHub Desktop.
ES6 OOP Example
This file contains hidden or 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' | |
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); |
This file contains hidden or 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' | |
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; | |
} | |
} |
This file contains hidden or 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' | |
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'); | |
} | |
} |
This file contains hidden or 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' | |
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