Javascript复合模式,不能正确使用覆盖的方法
Javascript Composite pattern, can not use overwritten methods correctly
我有一个javascript组合器模式,我实现了(见下面的代码)。
在我的主类中,我实例化了MenuItem或Menu。我必须在组件上调用方法update()
,它们应该返回相应的代码。
然而,它没有返回正确的totalitems
量。它总是返回MenuComponent
中定义的默认值0。
我认为这与this
关键字有关,但我找不到确切的解决方案。
菜单项:
//MENU ITEM
//----------
var MenuItem = function(id) {
MenuComponent.apply(this, [id, name]);
};
MenuItem.prototype = Object.create(MenuComponent.prototype);
MenuItem.prototype.constructor = MenuItem;
MenuItem.prototype.update = function() {
//works
console.log(this.ingredients)
//Doesnt work, this should display same as this.ingredients
console.log(this.calculateIngredients())
console.log("--------------")
};
菜单:
//MENU
//--------
var Menu = function(id, name) {
MenuComponent.apply(this, [id, name]);
this.menuitems = [];
};
Menu.prototype = Object.create(MenuComponent.prototype);
Menu.prototype.constructor = Menu;
Menu.prototype.add = function(menuitem) {
this.menuitems.push(menuitem);
};
Menu.prototype.remove = function(menuitem) {
for(var s, i = 0; s = this.getMenuItem(i); i++) {
if(s == menuitem) {
this.menuitems.splice(i, 1);
return true;
}
if(s.remove(menuitem)) {
return true;
}
}
return false;
};
Menu.prototype.getMenuItem = function(i) {
return this.menuitems[i];
};
Menu.prototype.calculateIngredients = function() {
this.ingredients = 0;
for(var key in this.menuitems) {
this.ingredients += this.menuitems[key].calculateIngredients();
}
return this.ingredients;
};
MenuComponent
//MenuComponent
//-------------
var MenuComponent = function(id, name) {
if(this.constructor === MenuComponent) {
throw new Error("Can't instantiate abstract class");
}
this.id = id;
this.name = name;
this.ingredients = 0;
};
MenuComponent.prototype.calculateIngredients = function() {
return this.ingredients;
};
MenuComponent.prototype.update = function() {
console.log(this.ingredients)
console.log("-----------------")
};
// HANDLER
var menuitem1 = new MenuItem(1)
, menuitem2 = new MenuItem(2)
, menuitem3 = new MenuItem(3)
, menuitem4 = new MenuItem(4)
, menuitem5 = new MenuItem(5)
, menuitem6 = new MenuItem(6)
, menuitem7 = new MenuItem(7)
, menu = new Menu(1);
menu.add(menuitem1);
menu.add(menuitem2);
menu.add(menuitem3);
menu.add(menuitem4);
menuitem1.ingredients = 1
menuitem2.ingredients = 5
menuitem3.ingredients = 7;
menuitem4.ingredients = 2
// lets say i want to update the ingredient count of the following
menuitem1.update();
menuitem2.update();
menu.update();
//the update goes wrong, it doesnt display the correct amount, it alwasy displays 0 on the amounts where i commented
JSFiddle
代替
MenuComponent.prototype.update = function() {
console.log(this.ingredients) // 0
};
您要呼叫
MenuComponent.prototype.update = function() {
console.log(this.calculateIngredients()) // 15
};
jsfiddle: http://jsfiddle.net/krzysztof_safjanowski/gjTb4/
相关文章:
- 如何覆盖主干中的extend方法
- 使用通用方法覆盖每个组合getValue字段
- 如何在循环中使用jQuery.on()方法,而不覆盖我循环的对象
- Javascript集:任何覆盖元素之间比较的方法
- 覆盖 WebResource javascript 方法 - WebForm_InitCallback()
- ES2015:从覆盖的类调用内部方法
- 是否可以检测父方法是否被覆盖
- 有没有任何方法可以覆盖浏览器渲染页面的方式
- 在IE8中恢复被覆盖的javascript方法
- javascript原生原型:扩展、添加和覆盖方法
- 覆盖原型方法
- Javascript 覆盖对象的方法
- XrayWrapper 错误(值是可调用的)在 Firefox 插件中覆盖原型方法时
- 在 ES6 中调用覆盖方法库
- jquery widget 中的覆盖方法
- 从不同的.js文件中访问 1 个.js源文件 (jqTree) 中的变量以覆盖方法
- 覆盖方法的 JavaScript 原型调用基方法
- 使用锚标记在 nodeJS 中覆盖方法 GET 以删除
- JavaScript 中的覆盖方法
- 如何使用本机(未覆盖)方法