A function factory is a function that creates and returns another function. It automates repeated function creation while applying shared behavior, such as logging or error handling. section 11.4
A factory makes reuse practical by:
- Wrapping different functions with the same logging and try/catch behavior. section 11.4
- Returning inner functions that preserve supplied configuration, as makeAdder does with n. section 11.4
- Returning the wrapped function’s result and adapting argument handling when needed. section 11.4
One logging factory, several functions
function wrapLogging(f) { return function(arg) { try { return f(arg); } catch (error) { logToSnapErrors(error); } }; } var saveUserDataWithLogging = wrapLogging(saveUserDataNoLogging); var fetchProductWithLogging = wrapLogging(fetchProductNoLogging); section 11.4
function makeAdder(n) {
return function(x) {
return n + x;
};
}
Compare a small factory that creates configured adders, making the returned function’s shared behavior easy to see. section 11.4










