Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

js destructuring macro

View gist:3881008
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 
// Note: there are bugs in hygienic renaming, so this doesn't work all the time yet.
 
macro varr {
case [$var (,) ...] = $expr => {
var i = 0;
var arr = $expr;
$(var $var = arr[i++];) ...
}
 
case {$var (,) ...} = $expr => {
var obj = $expr;
$(var $var = obj.$var;) ...
}
 
case $var:ident = $expr => {
var $var = $expr
}
}
 
varr [x, y, z] = [0, 1, 2];
console.log(x, y, z); // 0 1 2
 
varr {x, y, z} = {x: 5, y: 6, z: 7};
console.log(x, y, z); // 5 6 7
 
varr w = 10;
console.log(w); // 10

Note: Requires use of sweet.js.

Note: Requires use of sweet.js.

Can we replace varr with var by now as mentioned in your article?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.