Calling an Array of Functions in JavaScript(zpao.com)
zpao.com
Calling an Array of Functions in JavaScript
http://zpao.com/posts/calling-an-array-of-functions-in-javascript/
10 comments
I think the author comes to that conclusion, the article is just a thought experiment
Sure, I'm just making the point that sometimes when you look at something like that and think it can be improved, it's worth considering that the cure may end up being worse than the disease.
It is worth considering, but it's often worth finding out what the cure is before writing it off. You have to make the right tradeoffs (eg, when working on a tight deadline, it's probably not the right call to go off into the weeds finding out if you can make 3 lines of code into 1).
Sometimes, yes. And sometimes not. If, for instance, the author would have discovered that forEach worked differently, and there was some function that JS provides that worked cleanly with a readable name, then the article could have concluded that it makes sense to use that.
It also seems to be a bit more performant than some of the other methods (like `.shift()()` or `.call`). http://jsfiddle.net/brbcoding/hyw6a9kw/3/
Obviously, doing this is not super-readable, but it can be fun thought-experiments or brain-teasers.
There was a post I wrote about doing something similar a little while back [1]
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-methodsThe article laboriously avoids the words 'applicative functor' :)
A good abstraction does make the life easier time and again:
A good abstraction does make the life easier time and again:
> import Control.Applicative
> [(+1), (+20), (*10)] <*> [5]
[6,25,50]
> _Is this better?
> _.invoke(callbacks, 'call')
Short and concise, but needs underscore dependency.
> _.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.
Everybody knows what it does. Simple.