Created
October 28, 2016 12:37
-
-
Save forrestbe/fb143de0c9788ce471761cf5e2ba6142 to your computer and use it in GitHub Desktop.
Example of ES6 classes
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
| // ES5 | |
| var DJ = (function() { | |
| function MyConstructor(name) { | |
| this.name = name; | |
| } | |
| MyConstructor.prototype.speak = function speak() { | |
| console.log(this.name + ' is spinning some tunes.'); | |
| } | |
| return MyConstructor; | |
| })(); | |
| var dj = new DJ('Marcel Dettman'); | |
| dj.speak(); // Marcel Dettman is spinning some tunes | |
| // ES6 | |
| class Musician { | |
| constructor(name) { | |
| this.name = name; | |
| } | |
| speak() { | |
| console.log(this.name + ' is playing guitr.'); | |
| } | |
| } | |
| const musician = new Musician('Jimi Hendrix'); | |
| musician.speak(); // Jimi Hendrix is playing guitar. | |
| class Rapper extends Musician { | |
| speak() { | |
| super.speak(); | |
| console.log(this.name + ' has got bars.'); | |
| } | |
| } | |
| const rapper = new Rapper('Denzel Curry'); | |
| rapper.speak(); | |
| // Denzel Curry is playing guitar | |
| // Denzel Curry has got bars. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment