如何编写用javascript OOP编写的购物车代码

时间:2017-06-28 01:47:58

标签: javascript node.js oop

class ShoppingCart {

	function() {
		this.total = 0;
		this.items = {};
	}

	addItem(itemName, quantity, price) {
		this.total += price;
		this.items["itemName"] = quantity;	
	}

	removeItem(itemName, quantity, price) {
		if (quantity > this.items["itemName"]) {
			delete this.items["itemName"];
		}
		else {
			delete this.items["itemName"](this.items["itemName"] - quantity);
		}
		this.total -= price;
	}

	checkout(cashPaid) {
		if (cashPaid > this.total) {
			var balance = cashPaid - this.total;
			return "Your balance is " + balance;
		}
		else if (cashPaid < this.total) {
			return "Cash paid is not enough";
		}
		else {
			return "Thanks for shopping with us, Have a lovely day";
		}
	}

};

class Shop extends ShoppingCart {

	function() {
		this.quantity = 100;
	}

	removeItem() {
		this.items["itemName"]--; //decrements quantity of items by 1
	}

};

我收到以下错误:

TypeError:无法设置未定义的属性'itemName'

我正在运行它与节点v6.6.0,我也尝试在chrome控制台上运行它,它无法正常工作。事实是我还没有在Javascript中掌握OOP,请帮助我。感谢

0 个答案:

没有答案