最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - short syntax to call array of functions using lodash - Stack Overflow

matteradmin3PV0评论

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

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

Share Improve this question asked Apr 26, 2017 at 21:47 georgiy.zhuravlevgeorgiy.zhuravlev 4757 silver badges23 bronze badges 1
  • @AndrewLi ES5 used there, no arrow functions... – georgiy.zhuravlev Commented Apr 26, 2017 at 21:52
Add a ment  | 

1 Answer 1

Reset to default 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);
Post a comment

comment list (0)

  1. No comments so far