[Lab] Javascript 大小事試試 --- object method 綜合練習
這裡就不囉嗦,直接放code做個記錄。
var cashRegister = {
total: 0,
//insert the add method here
add: function(itemCost){
this.total += itemCost;
},
scan: function (item) {
switch (item) {
case "eggs":
this.add(0.98);
break;
case "milk":
this.add(1.23);
break;
//Add other 2 items here
case "magazine":
this.add(4.99);
break;
case "chocolate":
this.add(0.45);
break;
}
return true;
}
};
//Scan 2 eggs and 3 magazines
cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
//Show the total bill
console.log('Your bill is '+cashRegister.total);
var cashRegister = {
total: 0,
//insert the add method here
add: function(itemCost){
this.total += itemCost;
},
scan: function (item) {
switch (item) {
case "eggs":
this.add(0.98);
break;
case "milk":
this.add(1.23);
break;
//Add other 2 items here
case "magazine":
this.add(4.99);
break;
case "chocolate":
this.add(0.45);
break;
}
return true;
}
};
//Scan 2 eggs and 3 magazines
cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
//Show the total bill
console.log('Your bill is '+cashRegister.total);
留言
張貼留言