// 对象成员:属性,方法
// 属性:类似于变量
// 方法:类似于函数
const product = {
data: [
{ id: 1, name: "手机", price: 5000, num: 5 },
{ id: 2, name: "电脑", price: 4000, num: 15 },
{ id: 3, name: "相机", price: 14000, num: 10 },
],
getAmounts() {
return this.data.reduce((t, c) => (t += c.price * c.num), 0);
},
get total() {
return this.data.reduce((t, c) => (t += c.price * c.num), 0);
},
set setPrice(price) {
this.data[1].price = price;
},
};
console.log("总金额 = %d元 ", product.getAmounts());
console.log("总金额 = %d元 ", product.total);
console.log(product.data[1].price);
product.setPrice = 8000;
console.log(product.data[1].price);