ES6 function arrow without parameters

Considering

function f() { ... }

and another dosomething function that expects functions like f

function dosomething(callback) { ...; callback() } 

which f expects (dosomething example can be set Timeout)

calling dosomething and passing f, is there a difference between:

dosomething(f);

and

dosomething(() => f());

is any of these options preferable?

+4
source share
1 answer

That the wrapping function (second example) is an arrow function or does not change anything here.

However, this wrapping function can be useful to prohibit passing arguments: in the first case, if callbackcalled with an argument, it will be assigned f. Not in the second case. An alternative would be to limit the number of transmitted arguments dosomething((a, b) => f(a, b));.

this: doSomething bind f this (callback.bind(whatever)). ( ) , f this ( ) doSomething.

+4

All Articles