for-each loops iterates through the values of an array (or object). This is different from ordinary for-in loops which iterates through the indices (or property names) of the array/object.
For example:
for each (var x in [7, 9, 13]) write(x);Will write 7, 9, 13. An ordinary for-in loop (without each) would write 0,1,2. In most cases the for each behavior is what you need.
Destructuring assignments "unpacks" an array or object:
var [x, y] = [2, 4];sets x to 2 and y to 4.
Combining destructuring with for-each allows us to iterate through complex objects with a simple syntax:
var pairs = [[1,2], [3,4], [5,6]];I like these features because they remove boilerplate code, and makes some very common patterns clearer.
for each (var [a,b] in pairs) write(a+b);
Destructuring and for-each are already implemented natively in Mozilla, by the way.
2 kommentarer:
Found a bug:
package a {
(function() {
var foo = 1;
})();
}
Compilation failed.
1 messages:
Line: 0
Message:
Unexpected internal error, check log for additional info: TypeError('can only concatenate tuple (not "ProgramScope") to tuple',)
Thank you, Anonym! I am looking into it.
Send en kommentar