4 ms·
Yah but your example for Javascript uses some dubious code. Did you know that "for .. in" loops are also native to Javascript? Why not use one for JS, why only
by gavinlynch 15y ago
Yah but your example for Javascript uses some dubious code. Did you know that "for .. in" loops are also native to Javascript? Why not use one for JS, why only Coffeescript? Also, it seems like you are doing some useless variable declrations.
Let me rewrite it for you:
1)
var list = [{amount:2}, {amount:5}], i, sum = 0;
for (i = 0; i < list.length; i++) {
sum += list[i].amount;
}
console.log(sum);
2) Or let's go even more simple...
var list = [{amount:2}, {amount:5}], i, sum = 0;
for (i in list) {
sum += list[i].amount;
}
Personally, i don't find anything difficult about that.. Actually looks incredibly simple. What are you actually saving by using Coffeescript...? Some brackets, parens and a semi-colon? I don't know, those are just second nature for me. I've never really stopped and thought, "wow, if only I didn't have to type these extra parens, this is really killing me..."
Not once.
- henryl 15y agoJavaScript's for-in construct is not the same as iterating through all elements in the array as it will enumerate over all of the object's properties (see here: http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays http://stackoverflow.com/questions/500504/javascript-for-in-...) The other "complexities" in the compiled JavaScript are there for perf reasons (not accessing the "length" properties in the condition portion of the for loop, for example). Your comment underlies why some users find CoffeeScript useful in dealing with some of the idiosyncrasies and performance minutiae associated with JavaScript.
- gavinlynch 15y agoRight, for..in is not the same as iterating through all elements in an array. Understood. But it works excellent in the example the OP created and in my follow-up example. And I agree that accessing the length property in the conditional is not ideal. I guess it comes down to this for me: I don't feel that Javascript has an unduly burdensome list of common idiosyncrasies to justify developing in a 'different language'/syntax that compiles into javascript and changing my development workflow to reap said questionably useful benefits. Perhaps I am simply not the intended audience.