Created
July 22, 2016 02:40
-
-
Save Cheffheid/ec7aff15c5ffe64807da66d50aec0d2f to your computer and use it in GitHub Desktop.
ES5/ES6 classes
This file contains 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 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