const cart = {
contents: [],
addItem() {
}
};
cart.addItem("laptop");
console.log("The cart contains:", cart.contents);
如何将addItem方法中的项目放入content属性数组中?
const cart = {
contents: [],
addItem() {
}
};
cart.addItem("laptop");
console.log("The cart contains:", cart.contents);
如何将addItem方法中的项目放入content属性数组中?
Use
push
:If you want to pass multiple items like
addItem("laptop", "phone")
, use spreading:You could do like this
this.contents.push(item)