Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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 - Fatal编程技术网

Javascript 对象中的特定方法不起作用

Javascript 对象中的特定方法不起作用,javascript,Javascript,我正在用javascript构建一个小的收银机,我添加的一个方法有问题。似乎无法让它工作。我的控制台打印前两条语句,但不打印最后一条。你有什么想法吗 var cashRegister = { total: 0, add: function(itemCost) { this.total += itemCost; this.lastTransactionAmount = itemCost; }, scan: function(item,

我正在用javascript构建一个小的收银机,我添加的一个方法有问题。似乎无法让它工作。我的控制台打印前两条语句,但不打印最后一条。你有什么想法吗

var cashRegister = {
    total: 0,
    add: function(itemCost) {
        this.total += itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item, quantity) {
        switch (item) {
            case "eggs":
                this.add(0.98 * quantity);
                break;

            case "milk":
                this.add(1.23 * quantity);
                break;
        }
    },
    deleteLastTransaction: function() {
        this.total -= lastTransactionAmount;
    }
};
cashRegister.scan("eggs", 4);
cashRegister.scan("eggs", 2);
cashRegister.scan("milk", 2);
document.write("Total amount is: " + cashRegister.total + "</br>" + "Last transaction amount: " + cashRegister.lastTransactionAmount);
cashRegister.deleteLastTransaction();
document.write("The total amount is now: " + cashRegister.total);
var收银机={
总数:0,
添加:功能(项目成本){
此项为总成本+=项目成本;
this.lastTransactionMount=项目成本;
},
扫描:功能(项目、数量){
开关(项目){
案例“鸡蛋”:
加上(0.98*数量);
打破
案例“牛奶”:
增加(1.23*数量);
打破
}
},
deleteLastTransaction:函数(){
this.total-=上次交易量;
}
};
收银机扫描(“鸡蛋”,4);
收银机扫描(“鸡蛋”,2);
收银机扫描(“牛奶”,2);
单据.写入(“总金额为:“+CASHERGISTER.Total+”
“+”上次交易金额:“+CASHERGISTER.lastTransactionAmount”); cash register.deleteLastTransaction(); 文件。写入(“现在的总金额为:“+收银机。总计”);
没有变量
lastTransactionMount
。最后一个功能应该是:

deleteLastTransaction: function() {
    if ('lastTransactionAmount' in this) {
        this.total -= this.lastTransactionAmount;
    }
}

没有变量
lastTransactionMount
。最后一个功能应该是:

deleteLastTransaction: function() {
    if ('lastTransactionAmount' in this) {
        this.total -= this.lastTransactionAmount;
    }
}