Skip to content

Instantly share code, notes, and snippets.

@KSXGitHub
Created January 14, 2018 08:45
Show Gist options
  • Select an option

  • Save KSXGitHub/0a63a9ea19cb8a16e5c526e620c30130 to your computer and use it in GitHub Desktop.

Select an option

Save KSXGitHub/0a63a9ea19cb8a16e5c526e620c30130 to your computer and use it in GitHub Desktop.
Class-based OOP without "class" Raw
// 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