Hi! This blog is for news, announcements and questions regarding the
Mascara JavaScript compiler.

See also:
The online demo
Download latest release

Contact:
olav@olav.dk

Disclaimer:
ECMAScript is a trademark of Ecma International.

Powered by Squarespace
« Beta 5 released | Main | Beta 1 released »
Thursday
Dec042008

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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (2)

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

January 5, 2009 | Unregistered CommenterAnonymous

Thank you, Anonym! I am looking into it.

January 6, 2009 | Unregistered CommenterOlav Junker Kjær

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>