https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205
Last active
February 28, 2017 02:04
-
-
Save bdadam/b21582d3c9a4c039326cdc46819f2d44 to your computer and use it in GitHub Desktop.
Composition
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
const vehicleToHtml = obj => { | |
return `<div id="${obj.id}">${obj.name}</div>`; | |
}; | |
$.ajax({ | |
url: '/get/all/vehicles', | |
success: vehicles => { | |
const html = vehicles.map(vehicleToHtml); | |
// const html = vehicles.map(v => new Vehicle(v)).map(v => v.toHtml()); | |
document.querySelector('#list').innerHTML = html; | |
} | |
}); | |
// instead of: | |
class Vehicle { | |
constructor(data) { | |
this.id = data.id; | |
this.name = data.name; | |
} | |
toHtml() { | |
return `<div id="${this.id}">${this.name}</div>`; | |
} | |
} |
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
const db = { | |
find(query) {}, | |
findById(id) {}, | |
create(obj) {}, | |
delete(id) {} | |
}; | |
export default db; |
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
const createUser = db => ({ | |
create(userData) { db.create(userData); }, | |
delete(userID) {db.delete(userID); } | |
}); | |
export default createUser; |
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
import db from './3-db'; | |
import userModel from './4-userModel'; | |
const User = userModel(db); | |
User.create({ name: 'Mr. Qwe Asd', age: 100, city: 'Munich' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment