this
is pretty weird in JavaScript.
iterateAll: function() {
var self = this; // _this is also common
this.each(function(item) {
console.log(self.url);
console.log(item.url);
});
}
Basically, JavaScript doesn't formally distinguish between methods and functions. As a result, every function invocation -- even simple anonymous callbacks like these -- gets its own this
value. But this very frequently isn't...what anyone wants ever. So you can hold onto the "useful" value of this
in a temporary variable and reference it in the callback.
This is so common that it's a feature of CoffeeScript via the =>
syntax, and it's going to be a feature of ES6. Until then...temp variables are we've got.