Skip to content

Instantly share code, notes, and snippets.

@MikeBild
Last active October 30, 2016 06:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeBild/d5760386e20a7f9a14bd7d3accd0ca4a to your computer and use it in GitHub Desktop.
Save MikeBild/d5760386e20a7f9a14bd7d3accd0ca4a to your computer and use it in GitHub Desktop.
Memoize - you never need constructor/property based dependency injection in JavaScript
const require1 = require('./require1');
const require2 = require('./require2');
const require3 = require('./require3');
console.log(require1.getValue());
console.log(require2.getValue());
console.log(require3.getValue());
const memoize = require('lodash/memoize');
module.exports = memoize(createIt);
function createIt (param) {
let value = param || '';
return {
getValue: () => {
return value;
},
setValue: param => {
value = param
},
};
}
const A = require('./mod1')('A');
module.exports = A;
const AAgain = require('./mod1')('A');
AAgain.setValue('AA');
module.exports = AAgain;
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