-
-
Save ow/f2afc7fa23c0f0592082 to your computer and use it in GitHub Desktop.
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
class Animal { | |
constructor(race) { | |
this.race = race; | |
} | |
} | |
class Cat extends Animal { | |
constructor(race) { | |
super(race); | |
} | |
say() { | |
console.log(this.race + ' says "Meow!"'); | |
} | |
} | |
class Dog extends Animal { | |
constructor(race) { | |
super(race); | |
} | |
say() { | |
console.log(this.race + ' says "Bark!"'); | |
} | |
} | |
var cat = new Cat('cheshire') | |
, dog = new Dog('bulldog') | |
; | |
cat.say(); | |
dog.say(); | |
console.log(cat instanceof Cat); | |
console.log(cat instanceof Dog); | |
console.log(cat instanceof Animal); | |
console.log(dog instanceof Animal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice example. Thanks for sharing. I would suggest to refactor the property "race" and rename it to "breed"