Calling an Array of Functions in JavaScript
zpao.com
zpao.com
callbacks.forEach(function(callback) {
callback();
});
Everybody knows what it does. Simple.There was a post I wrote about doing something similar a little while back [1]
[1]: http://tech.pro/blog/2097/clever-way-to-demethodize-native-js-methodsA good abstraction does make the life easier time and again:
> import Control.Applicative
> [(+1), (+20), (*10)] <*> [5]
[6,25,50]
> _> _.invoke(callbacks, 'call')
Short and concise, but needs underscore dependency.
callbacks.forEach(Function.prototype.call.bind(Function.prototype.call));
It is just as long as the original and more convoluted, but much more fun.