Skip to content

Instantly share code, notes, and snippets.

@MikeBild

MikeBild/app.js

Last active Oct 30, 2016
Embed
What would you like to do?
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
You can’t perform that action at this time.