Vexi PlatformVexi Platform
vexi: easy, extensible, flexible

Saturday, August 29, 2009

Vexiscript Destructuring & Iterating Values

I have made a couple of improvements to vexi script (our dialect of javascript).

1) Destructurings

Destructurings in Vexscript are the same as those introduced in Mozilla Javascript 1.7.

//Useful for swapping values
var a = 1;
var b = 2;
[a,b] = [b,a];

// Or for handling multiple returns
var a,b,c;
[a,b,c] = f();


2)Improved for-in loop

Its pretty straightforward

var o = {a:3,b:2,c:1};
for(var k,v in o){
    // ...
}

// one might use this convention for an array
var arr = ["a","b","c"];
for(var i,v in o){
    // ...
}

This is not the same as any of the methods (there are at least 2) you can use in mozilla javascript. They support the form for(var [k,v] in o), which is unfortunate at is makes it an ambiguous syntax. It could be seen as a destructuring of just the key (so its not usually possible to have an array as a key in javascript, but its still not a very good choice in my opinion).

The other method is for each(var v in o), here the each key word just changes the behaviour of the loop. This is not really ideal as it is not useful if you need both the key and the value (which is the case perhaps as often as not).

No comments: