I'm curious if this is possible in JavaScript.
I want a factory function that can return functions whose actual contents are different. Say this is our factory:
function makeFunction(a, b, c) {
// Implement me
return f;
}
I want it to work like this:
> makeFunction(true, true, false);
=> function() { do_a(); do_b(); }
> makeFunction(false, true, false);
=> function() { do_b(); }
> makeFunction(true, false, true);
=> function() { do_a(); do_c(); }
Setting aside whether or not this is a good idea ... Can I do this? If so, what needs to happen inside makeFunction
?
Bonus: Generally, what is this pattern called?
Edit: I should clarify something I'm not looking for: the sane approach that uses closures. E.g. the following has the correct effect, but doesn't change the function contents/body:
function makeFunction(a, b, c) {
return function() {
if (a) { do_a(); }
if (b) { do_b(); }
if (c) { do_c(); }
}
}