Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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