Last active
October 30, 2016 06:22
-
-
Save MikeBild/d5760386e20a7f9a14bd7d3accd0ca4a to your computer and use it in GitHub Desktop.
Memoize - you never need constructor/property based dependency injection in JavaScript
This file contains hidden or 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 require1 = require('./require1'); | |
const require2 = require('./require2'); | |
const require3 = require('./require3'); | |
console.log(require1.getValue()); | |
console.log(require2.getValue()); | |
console.log(require3.getValue()); |
This file contains hidden or 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 memoize = require('lodash/memoize'); | |
module.exports = memoize(createIt); | |
function createIt (param) { | |
let value = param || ''; | |
return { | |
getValue: () => { | |
return value; | |
}, | |
setValue: param => { | |
value = param | |
}, | |
}; | |
} |
This file contains hidden or 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 A = require('./mod1')('A'); | |
module.exports = A; |
This file contains hidden or 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 AAgain = require('./mod1')('A'); | |
AAgain.setValue('AA'); | |
module.exports = AAgain; |
This file contains hidden or 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 B = require('./mod1')('B'); | |
B.setValue('BB'); | |
module.exports = B; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment