Something interesting to note about CoffeeScript.
Summary: {
In CoffeeScript, the fat arrow (=>) creates a closure that stores the reference to `this`. Every instance of @ is replaced with the original value of `this`. For example, the following code:
=>
@something
is equivalent to:
(function(_this) {
return (function() {
return _this.something;
});
})(this);
Notice the use of `_this.something`.
}
However, there's an interesting edge case worth mentioning:
=>
for a in something
for b in @something
something
This results in:
(function(_this) {
return (function() {
var a, b, i, len, results;
results = [];
for (i = 0, len = something.length; i < len; i++) {
a = something[i];
results.push((function() {
var j, len1, ref, results1;
ref = this.something;
results1 = [];
for (j = 0, len1 = ref.length; j < len1; j++) {
b = ref[j];
results1.push(something);
}
return results1;
}).call(_this));
}
return results;
});
})(this);
The issue here is that the inner loop loops through `this.something` instead of `_this.something`.
The specific lines causing this are:
ref = this.something;
results1 = [];
for (j = 0, len1 = ref.length; j < len1; j++) {
b = ref[j];
Is this normal behavior or a bug?