解构赋值
析构赋值写法
// 基础写法
var a = 1;
var b = 1;
var person = new object();
person.name = "kaso";
person.age = 20;
//进阶写法
var
a = 1,
b = 1;
var person = {
name : "kaso",
age : 20
}
// es6解构赋值
var [a, b] = [1,1];
var person = {name: 'xiaoliang', age: 15};
常用场景
1.两个变量交换值
// 传统写法
var x = 1;
var y = 2;
var k;
k = x;
x = y;
y = k;
// 解构赋值写法
var [x, y] = [1, 2];
[x, y] = [y, x]
2.快速获取当前页面域名与路径
var {hostname:host:domain, pathname:path} = location;
3.快速生成对象
var person = {
name: "kaso",
age: 20,
money: 1000
}
function buildPerson({year, month, day, hour=0, minute=0, second=0}) {
return new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second);
}
夹具