function add (a, b) { return a + b; } function multiply (a, b) { return a * b; } function applyFn (fn) { var parentArgs = Array.prototype.slice.call(arguments, 0); function wrapper () { var args = parentArgs.slice(1); args = args.concat(Array.prototype.slice.call(arguments, 0)); if (args.length >= 2) { return fn.apply(this, args); } return applyFn.apply(this, parentArgs.concat(args)); } return arguments.length-1 >= 2 ? wrapper() : wrapper; } var f = applyFn(multiply); console.log('3x5', f(3)(5)); console.log('2x8', f(2)(8)); f = applyFn(add); console.log('3+5', f(3)(5)); console.log('2+8', f(2)(8)); console.log('2+8', f(2)(8)(10));