Last active
July 25, 2018 20:42
-
-
Save mishelashala/47c92faa2f05988e44b2 to your computer and use it in GitHub Desktop.
OLOO Composition - JavaScript
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
'use strict'; | |
const Dog = { | |
name: 'Rocky', | |
age: 2 | |
}; | |
const createDog = (name, age) => Object.assign( | |
Object.create(Dog), | |
{ name, age } | |
) | |
exports.create = createDog; |
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
'use strict'; | |
const Dog = require('./dog'); | |
const Person = require('./person'); | |
// Factory methods! | |
const p = Person.create('Jenny Doe', 21); | |
const d = Dog.create('Puppy', 1); | |
// Composition over inheritance | |
p.dog = d; | |
console.log(d); // { name: 'Jenny Doe', age: 21, dog: { name: 'Puppy', age: 1 } } |
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
'use strict'; | |
const Dog = require('./dog'); | |
const Programmer = { | |
name: 'John Doe', | |
age: 20 | |
}; | |
const createProgrammer = (name, age) => Object.assign( | |
Object.create(Programmer), | |
{ name, age } | |
); | |
exports.create = createProgrammer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment