var cashRegister = {
total:0,
add: function(itemCost){
total += this.itemCost;
},
scan: function(item) {
switch (item) {
case "eggs":
this.add(0.98);
break;
case "magazine":
this.add(4.99);
break;
}
return true;
}
};
cashRegister.scan("eggs");
cashRegister.scan("magazines");
console.log('Your bill is '+cashRegister.total);
輸出顯示NAN,總計未定義。我在添加方法中嘗試了cashRegister.total
和this.total
,沒有運氣。上面的代碼有什麼問題?對象變量在javascript中不可訪問