Created
January 14, 2018 08:45
-
-
Save KSXGitHub/0a63a9ea19cb8a16e5c526e620c30130 to your computer and use it in GitHub Desktop.
Class-based OOP without "class" Raw
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
| // base class | |
| // - class is also constructor | |
| // - private properties are local variables that are not exposed (true private) | |
| // - public properties are object's properties | |
| const Animal = (age, sex) => ({ | |
| greet: () => '', | |
| getAge: () => age, | |
| getSex: () => sex ? 'male' : 'female' | |
| }) | |
| // subclass: Dog (extends Animal) | |
| const Dog = (...args) => { | |
| const greet = () => '' | |
| return { | |
| // overriding method: greet | |
| greet, | |
| // own method: bark | |
| bark: greet, | |
| // inherit from Animal | |
| __proto__: Animal(...args) | |
| } | |
| } | |
| // subclass: Human | |
| const Human = (age, sex, name) => ({ | |
| // overriding method: greet | |
| greet: () => `Greetings, I'm ${name}`, | |
| getName: () => name, | |
| // call super-constructor | |
| __proto__: Animal(age, sex) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment