简体   繁体   中英

Function that returns the invocation of its first parameter

On an array of functions, I essentially want to be able to run them all.

Kind of like this, functionArray.map(call) .

Is there any simple way to do this within JavaScript or Lodash other than the obvious (below)


functionArray.map(fn => fn())

Not all browsers support this yet


functionArray.map(function(fn) { return fn(); })

This is longer than it needs to be


function call(fn) { return fn(); })

defining a this function every time you want to use it, this seems like a silly solution, so a problem that might already be defined

I believe the 3rd option is your best bet:

function callSelf(fn) { return fn(); }
functionArray.map(callSelf)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM