Skip to content

Instantly share code, notes, and snippets.

@jlongster
Created October 12, 2012 19:28
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jlongster/3881008 to your computer and use it in GitHub Desktop.
Save jlongster/3881008 to your computer and use it in GitHub Desktop.
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
Copy link

Note: Requires use of sweet.js.

@JamesMGreene
Copy link

Note: Requires use of sweet.js.

@kristianmandrup
Copy link

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