Created
July 1, 2015 22:50
-
-
Save chuck0523/2df5bd10e528e0423334 to your computer and use it in GitHub Desktop.
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
| //Destructuring | |
| var [a, , b] = [1, 2, 3]; | |
| console.log(a, b); | |
| function today() { return {y: 2015, m: 4, d: 25}; } | |
| var {y: year, m: month} = today(); | |
| console.log(year, month); | |
| //関数の引数にも使える | |
| function dest({name: name}) { | |
| console.log(name); | |
| } | |
| var person = {id: 1, name: 'Taro'}; | |
| dest(person); // 'Taro' | |
| //値がなくてもエラーにはならない | |
| var [c, d] = [4]; | |
| console.log(c, d); // 4 undefined | |
| //値がなかった場合のデフォルト値も指定できる | |
| var [e = 1, f] = []; | |
| console.log(e, f); // 1 undefined | |
| //変数の値の入れ替えが楽 | |
| var a = 'bbb', b = 'aaa'; | |
| //ES5 | |
| var tmp = a; | |
| a = b; | |
| b = tmp; | |
| //ES6 | |
| [a, b] = [b, a]; | |
| console.log(a, b); ///aaa bbb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment