@2890594972
2018-04-13T06:02:20.000000Z
字数 1730
阅读 828
面向对象 1511
面向对象重构购物车-结构代码
Array.prototype.remove = function (val) {var index = this.indexOf(val);if (index > -1) {this.splice(index, 1);}};// 产品对象function Product(id, name, price, num, shopId){this.id = id;this.name = name;this.price = price;this.num = num;this.shopId = shopId;}// 购物车对象// 用来做什么: 管理数据,实现增删改查/** 管理数据,有增删改查* add, delete, edit, get.* isExit**/// 用来做什么: 管理事件,用来监听交互事件/*** initEvent* 店铺单个: check* 店铺全选: checkAll* 全选: checkTotalAll* 增加和减少: plus, minus*/// 用来做什么: 管理UI, 响应UI, 用来更新数据/*** 更新店铺价: uiShopTotalPrice,* 更新所有店铺: uiAllTotalPrice* 删除页面节点: uiDelete**/function Shopcar(){// 产品列表this.productList = [];}/*添加产品数据*/Shopcar.prototype.add = function(pro){this.productList.push(pro);}/*删除产品数据*/Shopcar.prototype.delete = function(id){var obj = this.get(id);this.productList.remove(obj);}/*获取产品数据*/Shopcar.prototype.get = function(id){// objvar obj = null;for(var i=0; i< this.productList.length; i++){if(this.productList[i]['id'] === id){obj = this.productList[i];}}return obj;}/*修改产品数据*/Shopcar.prototype.edit = function(id,pro){// 获取对象var obj = this.get(id);// 修改对象obj.num = pro.num;}/*创建产品*/ShopCar.prototype.createProduct = function ($shopInfo) {var shopId = $shopInfo.parents('.shop-group-item').data('id');var id = $shopInfo.data('id');var name = $shopInfo.find('h4').text();var num = +$shopInfo.find('.num').text();var price = +$shopInfo.find('.price').text();var pro = new Product(id, name, price ,num , shopId);return pro;}/*判断商品是否存在*/ShopCar.prototype.isExit = function(id){return this.get(id) === null ? false: true;}// 接近于MVC思想/*管理事件*/Shopcar.prototype.initEvent = function(){var that = this;// 选中产品$(".goodsCheck").click(function(){})// 全选店铺产品$(".shopCheck").click(function(){})// 店铺加$(".plus").click(function(){})// 店铺减$(".minus").click(function(){})// 全选$("#AllCheck").click(function(){})}/*管理ui渲染*/Shopcar.prototype.reRender = function(){}
