Skip to content

Instantly share code, notes, and snippets.

@forrestbe
Created October 28, 2016 12:37
Show Gist options
  • Select an option

  • Save forrestbe/fb143de0c9788ce471761cf5e2ba6142 to your computer and use it in GitHub Desktop.

Select an option

Save forrestbe/fb143de0c9788ce471761cf5e2ba6142 to your computer and use it in GitHub Desktop.
Example of ES6 classes
// 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