7

I believe, there is a shorter way (one line) to write this using lodash:

  _.forEach(eventListeners, function(callback) {
    callback(event);
  })

... but can't find yet

1
  • @AndrewLi ES5 used there, no arrow functions... Commented Apr 26, 2017 at 21:52

1 Answer 1

16

Lodash provides a utility function called _.over that returns a function that you can then call to pass some arguments to all of the functions you provided to _.over

Official documentation for _.over

var funs = [
  function(e) { console.log(e) },
  function(e) { console.log(e*2) },
  function(e) { console.log(e*3) }
];

_.over(funs)(10);

This will call all of the functions in the funs array with 10 as their argument, so in this case you should see in your console:

10
20
30

In your case specifically:

_.over(eventListeners)(event);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.