Re:Reducing Code Nesting
by ricardobeat on Monday January 02, 2012 @06:18AM
(#3415543)
I tend to favor one of these for clarity:
Array.prototype.push.bind(accum)
[].push.bind(accum)
(i realize the latter is wasteful, but arrays are so cheap that you wouldn't notice unless in a hot loop)> Also, whenever possible, I like to nix callbacks by using Function#bind:
There's one (and only one) non-DOM jquery method I enjoy for that sort of things, because it avoids repetition: $.proxy(Object, String)
accum.push.bind(accum)
becomes $.proxy(accum, 'push')
I think it spells out the intent better than the bind dance.Of course, things would still be better if javascript just bound method on instance prototype lookup.
Also, whenever possible, I like to nix callbacks by using Function#bind: