【发布时间】:2020-05-06 18:59:57
【问题描述】:
我创建了一个名为product 的类,其中totalcost 作为属性之一。当用户按下“添加”时,产品将被添加到名为products 的数组中。所以,我试图添加数组中每个对象的总成本来获得小计。但它只会将代码写出屏幕。
var subtotal = subtotal();
function subtotal() {
if (products.length == 1) {
subtotal = totalcost;
return subtotal;
} else if (products.length > 1) {
for (i = 0; i < products.length; i++) {
subtotal += products[i].totalcost + products[i + 1].totalcost;
return subtotal;
}
}
}
【问题讨论】:
-
这看起来不像java,看起来像js。此外,您还没有在任何地方声明
i或subtotal,这在两种语言中都是一个错误
标签: javascript arrays object properties