js面向对象的一点心得(套用)

2019-04-13 14:01发布

今天用面向对象的时候,遇到了一个这样得问题,我定义了两个模,第二个模得方法需要用到第一个模方法处理后得到的数据. 下面上代码: 模1:NameAndForm function NameAndForm(allName,orderForm){ this.allName = allName; this.orderForm = orderForm; } NameAndForm.prototype.get_ordered_name = function(){ var orderForm = this.orderForm; var orderedName = []; orderForm.forEach(function(list){ if(orderedName.indexOf(list.Name) == -1){ orderedName.push(list.Name) } }); return orderedName }; NameAndForm.prototype.get_not_ordered_name = function(){ var allName = this.allName; var orderedName = this.get_ordered_name(); var notOrderedName = []; allName.forEach(function(list){ if(orderedName.indexOf(list.Name) == -1){ notOrderedName.push(list.Name) } }); return notOrderedName };   模2:OrderFormController function show_order_list() { $.ajax({ url: "get_meal_list", type: "GET", dataType: "json", success: function (data) { var nameAndForm = new NameAndForm(data[1],data[0]); var orderFormController = new OrderFormController(nameAndForm.get_ordered_name(),nameAndForm.get_not_ordered_name(),data[0]); orderFormController.show_ordered_name_count(); orderFormController.show_ordered_list(); orderFormController.show_not_ordered_name_and_count(); orderFormController.show_total(); } }) } function OrderFormController(orderedName,notOrderedName,orderForm){ this.orderedName = orderedName; this.notOrderedName = notOrderedName; this.orderForm = orderForm; }    我此处data的传参包含一个对象和数组. 按模2的方法就可以很好的完成传参啦!