【发布时间】:2018-07-09 23:05:56
【问题描述】:
我有一个产品和商店课程。类 Product 具有属性名称、数量和价格。
class Product{
constructor(name, count, price){
this.name = name;
this.count = count;
this.price = price;
}
}
class Shop{
constructor(){
this.arrProducts = [];
}
addProduct (Product) {
let addName = Product.name;
let addPrice = Product.price;
let addCount = Product.count;
let newProduct = {name: addName, price: addPrice, count: addCount};
return this.arrProducts.push(newProduct);
};
}
我创建对象商店并添加产品
let shop = new Shop();
shop.addProduct(new Product("product1", 10, 200));
shop.addProduct(new Product("product2", 1, 500));
shop.addProduct(new Product("product3", 1, 1000));
之后我需要在表格中显示所有产品。我正在尝试这样做
let myTable= "<table><tr><td style='width: 100px; color: red;'>Product Name</td>";
myTable+= "<td style='width: 100px; color: red; text-align: right;'>Price</td>";
myTable+="<td style='width: 100px; color: red; text-align: right;'>Count</td></tr>";
myTable+="<tr><td style='width: 100px; '>---------------</td>";
myTable+="<td style='width: 100px; text-align: right;'>---------------</td>";
myTable+="<td style='width: 100px; text-align: right;'>---------------</td></tr>";
for (let item in shop) {
myTable+="<tr><td style='width: 100px;'>" + shop[item].name + "</td>";
myTable+="<td style='width: 100px; text-align: right;'>" + shop[item].price + "</td>";
myTable+="<td style='width: 100px; text-align: right;'>" + shop[item].count + "</td></tr>";
}
myTable+="</table>";
document.write( myTable);
但我没有定义。
【问题讨论】:
-
请同时添加
Shop。 -
我添加了
Shop和方法addProduct -
恕我直言,使用像
ejs或moustachejs这样的q 服务器端模板库会容易得多。自动化模板构建过程会更好。