Javascript 方法覆盖的敲除中的类继承
我正在尝试在Knockout中进行类继承,以应用DRY原则,但没有成功 非常成功 我试图实现的是两种类型的服务器,它们在不重复代码或大量代码的情况下共享一些相似之处 这是我的最新尝试,有些功能无法正常工作 父类:Javascript 方法覆盖的敲除中的类继承,javascript,inheritance,knockout.js,overriding,prototype,Javascript,Inheritance,Knockout.js,Overriding,Prototype,我正在尝试在Knockout中进行类继承,以应用DRY原则,但没有成功 非常成功 我试图实现的是两种类型的服务器,它们在不重复代码或大量代码的情况下共享一些相似之处 这是我的最新尝试,有些功能无法正常工作 父类: function BaseServer(options, builder) { this.formatted_price = ko.computed(function() { return utils.format_price(this.price(), options.
function BaseServer(options, builder) {
this.formatted_price = ko.computed(function() {
return utils.format_price(this.price(), options.prices.currency());
}, this);
this.drives = ko.observableArray([]);
this.number_of_instances = ko.observable(1);
// Not sure if this is a good approach for this problem
for (i = 0; i < options.ssd.length; i++) {
self.drives.push(new builder.ssd(options.ssd[i], options.prices));
}
for (i = 0; i < options.hdd.length; i++) {
self.drives.push(new builder.hdd(options.hdd[i], options.prices));
}
// Will not work there is not this.cpu nor this.ram
this.price = ko.computed(function() {
var total = 0
total += this.cpu.price();
total += this.ram.price();
total += _.reduce(this.drives(), function(mem, drive) {
return drive.price();
}, 0);
return total;
}, this);
}
我认为事情不对:
- 来自父级的格式化\u价格不会使用来自BetaServer类的此覆盖
- 也许我可以用另一种方式构造磁盘
var SuperClass = function(options) {
var self = this;
options = options || {};
self.value = ko.observable(options.value);
self.computed = ko.computed(function() {
return self.value() + 100;
});
};
var SubClass = function(options) {
var self = this;
// call super constructor
self.constructor.call(self, options);
// save method from super
var super_computed = self.computed;
// overwrite method on child class
self.computed = ko.computed(function() {
// use saved super method in the overwritten method
return super_computed() + 5;
});
};
SubClass.prototype = new SuperClass();
var sub = new SubClass({ value: 1 });
console.log(sub.computed()); // should be 106
var sub2 = new SubClass({ value: 2 });
console.log(sub2.computed()); // should be 107
这里有一个JSFIDLE显示了这个方法:这并不能回答您所有的问题,但下面是一个我更喜欢如何处理继承和覆盖方法的示例:
var SuperClass = function(options) {
var self = this;
options = options || {};
self.value = ko.observable(options.value);
self.computed = ko.computed(function() {
return self.value() + 100;
});
};
var SubClass = function(options) {
var self = this;
// call super constructor
self.constructor.call(self, options);
// save method from super
var super_computed = self.computed;
// overwrite method on child class
self.computed = ko.computed(function() {
// use saved super method in the overwritten method
return super_computed() + 5;
});
};
SubClass.prototype = new SuperClass();
var sub = new SubClass({ value: 1 });
console.log(sub.computed()); // should be 106
var sub2 = new SubClass({ value: 2 });
console.log(sub2.computed()); // should be 107
这里有一个JSFIDLE显示了这个方法:这并不能回答您所有的问题,但下面是一个我更喜欢如何处理继承和覆盖方法的示例:
var SuperClass = function(options) {
var self = this;
options = options || {};
self.value = ko.observable(options.value);
self.computed = ko.computed(function() {
return self.value() + 100;
});
};
var SubClass = function(options) {
var self = this;
// call super constructor
self.constructor.call(self, options);
// save method from super
var super_computed = self.computed;
// overwrite method on child class
self.computed = ko.computed(function() {
// use saved super method in the overwritten method
return super_computed() + 5;
});
};
SubClass.prototype = new SuperClass();
var sub = new SubClass({ value: 1 });
console.log(sub.computed()); // should be 106
var sub2 = new SubClass({ value: 2 });
console.log(sub2.computed()); // should be 107
这里有一个JSFIDLE显示了这个方法:这并不能回答您所有的问题,但下面是一个我更喜欢如何处理继承和覆盖方法的示例:
var SuperClass = function(options) {
var self = this;
options = options || {};
self.value = ko.observable(options.value);
self.computed = ko.computed(function() {
return self.value() + 100;
});
};
var SubClass = function(options) {
var self = this;
// call super constructor
self.constructor.call(self, options);
// save method from super
var super_computed = self.computed;
// overwrite method on child class
self.computed = ko.computed(function() {
// use saved super method in the overwritten method
return super_computed() + 5;
});
};
SubClass.prototype = new SuperClass();
var sub = new SubClass({ value: 1 });
console.log(sub.computed()); // should be 106
var sub2 = new SubClass({ value: 2 });
console.log(sub2.computed()); // should be 107
这里有一个JSFiddle显示了这个方法:如果我想在js中使用“类”,我将转向:
请参见如果我想要js中的“类”,我将转向:
请参见如果我想要js中的“类”,我将转向:
请参见如果我想要js中的“类”,我将转向:
看我得试试这个。但它看起来真的像我要找的。Js在如何构建类方面拥有大量资源,因此很难找到解决常见问题的确定模式。非常感谢查理,我得试试这个。但它看起来真的像我要找的。Js在如何构建类方面拥有大量资源,因此很难找到解决常见问题的确定模式。非常感谢查理,我得试试这个。但它看起来真的像我要找的。Js在如何构建类方面拥有大量资源,因此很难找到解决常见问题的确定模式。非常感谢查理,我得试试这个。但它看起来真的像我要找的。Js在如何构建类方面拥有大量资源,因此很难找到解决常见问题的确定模式。非常感谢查理。