This piece of CoffeeScript:
for i in [1..10]
console.log i
is compiled to:
for (i = _i = 1; _i <= 10; i = ++_i) {
console.log(i);
}
I don't understand why it doesn't just use an i
. Any ideas?
You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.
I'm not very familiar with CoffeeScript, but my guess is that it is to prevent modification of the i
variable within the loop.
For instance:
for i in [1..10]
console.log i
i = 7
might have produced this code
for (i = 1; i <= 10; ++i) {
console.log(i);
i = 7;
}
This obviously produces an infinite loop.
CoffeeScript's version, however, means this happens:
for (i = _i = 1; _i <= 10; i = ++_i) {
console.log(i);
i = 7;
}
The loop is no longer infinite because of the presence of _i
to track the position in the loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With