Skip to content

Instantly share code, notes, and snippets.

@ulisesantana
Created July 7, 2018 12:11
Show Gist options
  • Save ulisesantana/10146515c76c396686c363ae2979dc92 to your computer and use it in GitHub Desktop.
Save ulisesantana/10146515c76c396686c363ae2979dc92 to your computer and use it in GitHub Desktop.
Proof of concept about inheritance in JS without classes
function Animal(animalName, animalType) {
let name = animalName;
const type = animalType;
return {
getName() {
return name;
},
setName(newName) {
name = newName;
},
introduceItself() {
console.log(`My name is ${name} and I am a ${type}`);
}
};
}
function Dog(name) {
const s = Animal(name, 'dog');
return {
...s,
introduceItself() {
console.log('Woff!!');
},
formalIntroduce() {
s.introduceItself();
}
};
}
const ulises = Animal('Ulises', 'Human');
const linda = Dog('Linda');
console.log(ulises.getName()); // Ulises
linda.introduceItself(); // Woff!!
linda.formalIntroduce(); // My name is Linda and I am a dog
linda.setName('Ambrosia');
console.log(linda.getName()); // Ambrosia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment