0

在下面的代码中,我创建了一个构造函数。我想添加一个叫做total of two objects 的值(一个叫做candy,另一个叫做computer)。他们显然不会添加......他们连接。但是,当我执行乘法时,它会按预期相乘。我需要做什么才能对它们进行添加?

代码如下。对于我的示例,我使用乘法并评论了问题区域。

<!DOCTYPE html> 
<meta charset="UTF-8"> 
<title>checkoutTemplate</title>

<script>
function Item (item,price,tax,addedFee,total){
    this.item = item;
    this.price = price;
    this.tax = function () {
    if (typeof tax == 'undefined')
    {
        tax = price * .10;          
    } 
    else {
        tax = 0;
    }
    return tax;
    };

    this.addedFee = function () {
    if (typeof addedFee == 'undefined')
    {
        addedFee = price * .05;         
    } 
    else {
        addedFee = 0;
    }
    return addedFee;
    };


    this.total = function () {return this.price+ this.tax()+ this.addedFee()};


};



var computer = new Item("computer", 1000,"NA","NA");
var candy = new Item("candy", 0.50,"NA","NA");


document.write( "the total output value: " + candy.total() * computer.total() ); // Troubled area. I want to add candy.total and computer.total.Not multiply. 

非常感谢你

4

1 回答 1

2

用于()让 number plus 先执行:

"the total output value: " + (candy.total() + computer.total())
于 2012-09-18T02:25:14.293 回答