Created
October 12, 2012 19:28
-
-
Save jlongster/3881008 to your computer and use it in GitHub Desktop.
js destructuring macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Requires use of sweet.js.