torsdag den 4. december 2008

Beta 4 released

Beta 4 fixes a few bugs, most notably destructuring assignments now works in for-each loops. This is notable since it combines two of my favorite features!

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]];
for each (var [a,b] in pairs) write(a+b);
I like these features because they remove boilerplate code, and makes some very common patterns clearer.

Destructuring and for-each are already implemented natively in Mozilla, by the way.

2 kommentarer:

Anonym sagde ...

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',)

Olav Junker Kjær sagde ...

Thank you, Anonym! I am looking into it.

This blog has moved to http://blog.mascaraengine.com. See you!

This blog is (was) for news, announcements and questions regarding the
Mascara ECMAScript 6 -> JavaScript translator.