1)数组解构
2) 对象解构
let item = { id: 10, name: “mobiltelephone” };
let id = item.id;
let name = item.name;
console.log("id = %d, name = %s", id, name);
3)传参解构
const product = {
data.[
{id: 1, name: “computer”, price: 1000, num: 5 },
{id: 2, name: “camera”, price: 2000, num: 5 },
],
getAmounts(){
return this.data.reduce((t, c) => (t += c.price c.num), 0);
},
};
console.log(“sum = %d dollas”, product.getAmounts());
get total () {
return this.data.reduce((t, c) => (t += c.price c.num), 0);
访问器属性的的优先级高于同名的普通属性
let user = {
data: {name},
get name() {
return this.data.name;
};
set name(v) {
this.data.name = v;
},
};
user.name = "Peter";
console.log(user.name);