Create a gist now

Instantly share code, notes, and snippets.

Embed
js destructuring macro
// 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
@JamesMGreene

This comment has been minimized.

Show comment
Hide comment
@JamesMGreene

JamesMGreene Oct 13, 2012

Note: Requires use of sweet.js.

Note: Requires use of sweet.js.

@JamesMGreene

This comment has been minimized.

Show comment
Hide comment
@JamesMGreene

JamesMGreene Oct 13, 2012

Note: Requires use of sweet.js.

Note: Requires use of sweet.js.

@kristianmandrup

This comment has been minimized.

Show comment
Hide comment
@kristianmandrup

kristianmandrup Nov 14, 2014

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

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