Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对使用构造函数创建的javascript对象方法执行添加_Javascript_Methods - Fatal编程技术网

如何对使用构造函数创建的javascript对象方法执行添加

如何对使用构造函数创建的javascript对象方法执行添加,javascript,methods,Javascript,Methods,在下面的代码中,我创建了一个构造函数。我想添加两个对象(一个称为糖果,另一个称为计算机)的名为“总计”的值。它们显然不会添加…它们连接在一起。但是,当我执行乘法时,它会按预期进行乘法。我需要做些什么来执行对它们的添加 代码如下。在我的示例中,我使用乘法,并对问题区域进行了注释 <!DOCTYPE html> <meta charset="UTF-8"> <title>checkoutTemplate</title> <script>

在下面的代码中,我创建了一个构造函数。我想添加两个对象(一个称为糖果,另一个称为计算机)的名为“总计”的值。它们显然不会添加…它们连接在一起。但是,当我执行乘法时,它会按预期进行乘法。我需要做些什么来执行对它们的添加

代码如下。在我的示例中,我使用乘法,并对问题区域进行了注释

<!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. 

签出模板
功能项(项目、价格、税费、附加费、总计){
this.item=项目;
这个价格=价格;
this.tax=函数(){
如果(税种==‘未定义’)
{
税=价格*.10;
} 
否则{
税=0;
}
退税;
};
this.addedFee=函数(){
if(typeof addedFee==“未定义”)
{
addedFee=价格*.05;
} 
否则{
addedFee=0;
}
返回addedFee;
};
this.total=function(){返回this.price+this.tax()+this.addedFee()};
};
var计算机=新项目(“计算机”,1000,“不适用”,“不适用”);
var candy=新项目(“candy”,0.50,“NA”,“NA”);
document.write(“总输出值:+candy.total()*computer.total());//动乱地区。我想把candy.total和computer.total相加,而不是相乘。

非常感谢

使用
()
让number plus先执行以下操作:

"the total output value: " + (candy.total() + computer.total())

我在发帖前试过这个。我想这是我的语法错误。谢谢