5 ms·
All those additions are welcome (and wayyyyyyyyy overdue to be honest) but something is striking me as quite old-fashioned : for (var [key, val] of items(x))
by VeejayRampay 12y ago
All those additions are welcome (and wayyyyyyyyy overdue to be honest) but something is striking me as quite old-fashioned :
for (var [key, val] of items(x)) { alert(key + ',' + val); }
That syntax isn't really natural, I would think something like:
items(x).forEach(function(key, value) {
alert(key + ',' + val);
});
is more natural and readable. It also brings up the question of interpolation, why do we even have to use + in the first place?
Also, can someone in the know care to tell me why that is that those additions for what is the end rather basic functionality is coming so late to the language?
- chuckup 12y ago>Also, can someone in the know care to tell me why that is that those additions for what is the end rather basic functionality is coming so late to the language? I would think Internet Explorer being the dominant browser had something to do with it.
- espadrine 12y agoIterables can have many forms (Strings, Arrays, Maps…), so having a forEach method is tricky. On the other hand, most iterables have their own methods to loop around values. Arrays and Maps both have a .forEach() method.
- laughinghan 12y agoitems(x).forEach(function(key, value) { alert(key + ',' + val); }); You can already just do that, without ES6. > It also brings up the question of interpolation, why do we even have to use + in the first place? Did you miss the last item, "quasis"? `You are ${age} years old.` > Also, can someone in the know care to tell me why that is that those additions for what is the end rather basic functionality is coming so late to the language? Because unlike most languages, there's no canonical implementation. Mozilla, who actually holds the trademark on the name "JavaScript", totally tried to evolve the language since the beginning and even shipped e.g. JavaScript 1.7 (which had generators, iterators, let, array comprehensions, and destructuring assignment [1]) with Firefox 2 in 2006, but Firefox just didn't have the market share for authors to start using the new features, and no other browser implemented any of them. That deadlock was only broken by Brendan Eich using corporate politics rather than hacker-style "putting it out there": rounding up all the big browser makers (of course, it helped that Apple and Google had just broken into the scene), and convincing them to commit to implementing new features decided on by the committee they formed, TC39 (and to update their browsers at all, ahem IE ahem). [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7 https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_...
- VeejayRampay 12y agoUnless I'm missing something, you cannot use forEach on a Javascript object as of now (or map, reduce, every, some and other methods that are available on Array.prototype). The only way is to use: for(var key in obj) {...} As for interpolation, I indeed missed the bit about quasis. Though I really wonder why they would still use concatenation if they can interpolate... Maybe to only show one feature at a time, which I guess makes sense.
- arcosdev 12y agoYou are correct, there is no forEach, a lot of libraries implement their own, but no native version.
- SimeVidas 12y agoObject.keys(obj).forEach(function (key) { var val = obj[key]; // use key and val here });
- omaranto 12y agoI think you did miss something: the comment you replied to didn't say x.forEach(f); but rather items(x).forEach(x); which is definitely possible, you just need to implement items().
- carussell 12y agoSun/Oracle has always held the trademark on JavaScript, not Mozilla. And TC39 predates the "JavaScript Renaissance" that you're talking about.
- laughinghan 12y ago"JavaScript is a trademark or registered trademark of Sun Microsystems, Inc. [...] used under license." https://www.mozilla.org/en-US/foundation/trademarks/list/ https://www.mozilla.org/en-US/foundation/trademarks/list/ TIL! And you're right about TC39, I probably should have said "joined", not "formed", if I could edit my comment I would. I don't think my comment was a gross misrepresentation of the historical context, though.
- gsnedders 12y ago> Also, can someone in the know care to tell me why that is that those additions for what is the end rather basic functionality is coming so late to the language? Back when IE was stagnating, when nobody was working on anything more than maintaining IE6, TC-39 (the technical committee that works on ECMAScript) was working towards a 4th revision of the standard (i.e., ES4), incorporating a lot of quite reasonable things (like the above) but also quite a lot of things based on very recent research. There was pushback against quite so aggressively moving forward in a single revision, especially incorporating so much from academia not already proven elsewhere (the browser space is very conservative about breaking existing content, so if something that turned out to be a bad idea shipped, it'd be very very hard to get rid of). In the end, there was relatively significant pushback once MS reappeared from themselves, Yahoo, and a number of other contributors. Moving forward, the plan was essentially to tidy up the ES3 spec with minor additions, which was originally referred to as ES3.1 but eventually standardised as ES5. ES6 is the next revision of this branch of the standard. ActionScript 3, as Flash uses, is based on one of the drafts of ES4, and prior to the abandonment of Flash development, the intention was to continue to evolve the language separately to ECMAScript.