Mozilla introduced this back in Firefox 2, and you can replace var with it, so TFAA's
for (var i=0, ilen=elements.length; i<ilen; i++) {
var element = elements[i];
let (num = i) {
LIB_addEventListener(element, function(event) {
alert('I was originally number ' + num);
});
};
}
becomes: for (let i=0, ilen=elements.length; i<ilen; i++) {
let element = elements[i];
LIB_addEventListener(element, function(event) {
alert('I was originally number ' + i);
});
}
Of course, since this does not quite seem to be a tight loop you should be using Array.prototype.forEach instead: elements.forEach(function (element, i) {
LIB_addEventListener(element, function(event) {
alert('I was originally number ' + i);
});
});
> The JavaScript idiom that potentially spans the most lines of your program may be the inheritance idiom.That's not an idiom. And there are numerous implementations of classes on top of prototypes out there, if there's one javascript "problem" which does not need new syntax it's that one.