Created
July 7, 2018 12:11
-
-
Save ulisesantana/10146515c76c396686c363ae2979dc92 to your computer and use it in GitHub Desktop.
Proof of concept about inheritance in JS without 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
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