tldr-again; The author of CoffeeScript just told me I'm right: Don't use _i
.
14:29 <jashkenas> You shouldn't use internal variables. ... 14:42 <meagar> I was hoping something more deeply involved in the language would be able to put some authority behind that opinion 14:43 <meagar> ... I was basically hoping for an authoritative "don't do that" 14:44 <jashkenas> you just got it ;) 14:44 <jashkenas> for item, index in list -- there's your reference to the index.
tldr; This is at best an undocumented feature for which a functionally equivalent documented feature exists. As such, it should not be used.
Your argument of "less typing" is highly dubious; compare:
for x in [1, 2, 3] when _i % 2 == 0
console.log "#{_i} -> #{x}"
for x,i in [1, 2, 3] when i % 2 == 0
console.log "#{i} -> #{x}"
feature, bug, or NaN?
None of these things; it's undefined behaviour. You are assuming that _i
will be the variable used for iteration in the compiled JavaScript.
You definitely shouldn't use _i
, or assume _i
will be defined. That's an implementation detail, and they're free to change it at any time. It's also won't be _i
if your loop is nested in another loop; it will be _j
or _k
etc.
Most importantly, you can do this exact thing without relying on the underlying implementation's JavaSript variables. If you want to loop with an index, just use for value,key in array
:
array = ['a', 'b', 'c']
console.log(index) for item, index in array # 0, 1, 2
Specifically, in your example:
feast = (cats) -> eat cat for cat, index in cats when index % 2 == 0