Skip to content

Instantly share code, notes, and snippets.

@Cheffheid
Created July 22, 2016 02:40
Show Gist options
  • Save Cheffheid/ec7aff15c5ffe64807da66d50aec0d2f to your computer and use it in GitHub Desktop.
Save Cheffheid/ec7aff15c5ffe64807da66d50aec0d2f to your computer and use it in GitHub Desktop.
ES5/ES6 classes
// ES5 does technically not have classes, and so the closest approximation would be something like this:
function Animal( name ) {
this.name = name;
}
function Dog() {
Animal.call( this, "Dog" );
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.bark = function bark() {
alert("Woof!");
}
// ES6 adds a class keyword and allows you to "mimic" classical inheritance, but ultimatly it's just syntactic sugar.
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor() {
super("Dog");
}
bark() {
alert("Woof!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment