Depending on the context, both methods are equally important, although I would suggest that the latter form is used less. When to use one above the other? Simple, if you need to pass arguments that are immediately available, use the first form. If one of the arguments does not change and the caller expects a function, use the second form. Consider this code, I believe this clearly illustrates my point:
function add_direct(a, b) { return a + b; } function add_functor(a) { return function (b) { return a + b; } } var nums = [1,4,8]; var add3 = add_functor(3); console.log(nums.map(add3)); console.log(nums.map(n => { return add_direct(3, n); }));
Both of them add 3, but the second form makes sense here. Both have their own strengths and weaknesses. Hope this clears the air.
PS: In C ++, the second form is called Functor ; Others call them closing or Function Objects .
Rafael
source share