Comparing two different ways to do dependency injection
Simple factory (straight forward)
const createStuff = (dependencies) => {
const newStuff = {
//... using the dependencies
}
return newStuff;
};
Function binding (arguably make the module more readable)
// stuff.js
const stuffApi = {
f1(dependencies, ...args) {
// using deps
},
f2(dependencies, ...args) {
// using deps
}
}
// createStuff
// exemple implementation, do not use
const createStuff = (deps) => {
return Object.assign({}, Object.keys(stuff).reduce((acc, key) => {
if (typeof acc[key] === 'function') {
acc[key] = stuff[key].bind(acc, deps);
} else {
acc[key] = stuff[key]
}
return acc
}, {}));
}
Would love to get an idea on what people think of these two patterns :)