Re:But – A way to pass along a callback with just a single a...
by willyg302 on Monday December 29, 2014 @03:35PM
(#8811634)
Additionally, this will log all the passed arguments (value, index, and array being traversed), which typically isn't what you want:
[1, 2, 3].forEach(console.log.bind(console));
> 1 0 [1, 2, 3]
> 2 1 [1, 2, 3]
> 3 2 [1, 2, 3]
Re:But – A way to pass along a callback with just a single a...
by dyscrete on Monday December 29, 2014 @03:32PM
(#8811623)
Well it's not just about `this` but the arguments to pass to console rather. In this case:
n.forEach(console.log.bind(console));
forEach is passing console all of its callback arguments, resulting in logging of keys, values, and array.EDIT: Forgot array
Re:But – A way to pass along a callback with just a single a...
by nilved on Monday December 29, 2014 @03:55PM
(#8811716)
Ah, I understand. It reminds me of how the `cut` function could be used in Scheme.
Re:But – A way to pass along a callback with just a single a...
by aaronem on Monday December 29, 2014 @03:59PM
(#8811739)
Convenience, I suppose; I had a few headaches when I first transitioned to Javascript and discovered the odd signatures its collection functions' callbacks are expected to have.
e: was, in fact, missing something. Apologies for the disparaging comment, see replies.