4 ms·
> If your application uses the let idiom, wouldn’t it be nice to have new syntax for it? Mozilla introduced this back in Firefox 2, and you can replace var wit
by masklinn 15y ago
> If your application uses the let idiom, wouldn’t it be nice to have new syntax for it?
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.
- jacabado 15y agoYour example shows how the author's conclusions are wrong. IMHO the biggest milestones of Javascript life have been his libraries and the creative uses we have seen around them. From XMLHttpRequest to jQuery (and Prototype, Mootools, etc) and now Node.JS there have been highly disruptive innovations over this basically flawed language. Maybe we should give more attention to the basic process behind this, creating libraries aka Programming Motherfucker.
- bzbarsky 15y agoActually, the forEach version can end up faster than the let version... especially if you self-host forEach so that the JIT can do its job (e.g. inlining the LIB_addEventListener calls so there aren't actually any function invocations, etc).
- masklinn 15y ago> Actually, the forEach version can end up faster than the let version... Theoretically maybe, but I've yet to see that. A regular `for(;;)` is generally 3-5 times faster than using `Array.prototype.forEach`. Re. JIT, hand-rolling a foreach in javascript can be up to twice as fast as the native one, depending on the features you leave out.
- bzbarsky 15y ago> Theoretically maybe, but I've yet to see that Compare the let version to the code in https://bugzilla.mozilla.org/show_bug.cgi?id=602132 https://bugzilla.mozilla.org/show_bug.cgi?id=602132