我的Javascript继承方法有什么问题?
我知道有很多方法可以实现JS继承。我试图在这里做一些事情,在这里我将行传递到我的子类中,我希望在构造函数时将其传递到超类:我的Javascript继承方法有什么问题?,javascript,Javascript,我知道有很多方法可以实现JS继承。我试图在这里做一些事情,在这里我将行传递到我的子类中,我希望在构造函数时将其传递到超类: function AbstractTableModel(rows) { this.showRows = function() { alert('rows ' + this.rows); } } function SolutionTableModel(rows) { this.prototype = new AbstractTa
function AbstractTableModel(rows) {
this.showRows = function() {
alert('rows ' + this.rows);
}
}
function SolutionTableModel(rows) {
this.prototype = new AbstractTableModel(rows);
this.show = function() {
this.protoype.showRows();
};
}
var stm = new SolutionTableModel(3);
stm.show();
小提琴:
它似乎不起作用,因为原型方法没有向下流动:(有什么想法吗?
首先,您必须定义this.rows
function AbstractTableModel(rows) {
this.rows = rows;
this.showRows = function() {
alert('rows ' + this.rows);
};
}
其次,如果您想从AbstractTableModel
继承,您应该这样做
function SolutionTableModel(rows) {
AbstractTableModel.call(this, rows);
this.show = function() {
this.showRows();
};
}
SolutionTableModel.prototype = new AbstractTableModel();
var stm = new SolutionTableModel(3);
stm.show();
/================================================================================================================================================/
如果希望避免调用基构造函数两次,也可以使用寄生组合继承模式:
function inheritPrototype(subType, superType) {
var prototype = Object.create(superType.prototype, {
constructor: {
value: subType,
enumerable: true
}
});
subType.prototype = prototype;
}
function AbstractTableModel(rows) {
this.rows = rows;
this.showRows = function () {
alert('rows ' + this.rows);
};
}
function SolutionTableModel(rows) {
AbstractTableModel.call(this, rows);
this.show = function () {
this.showRows();
};
}
inheritPrototype(AbstractTableModel, SolutionTableModel);
var stm = new SolutionTableModel(3);
stm.show();
这是进行对象继承的一种方法。在我看来,这种方法是最优雅的,可以找到详细信息
以下是一个基于您所做工作的工作示例:
function AbstractTableModel(rows) {
this.showRows = function () {
alert('rows ' + rows);
}
}
function SolutionTableModel(rows) {
var self = this;
this.prototype = new AbstractTableModel(rows);
this.show = function () {
self.prototype.showRows();
};
}
var stm = new SolutionTableModel(3);
stm.show();
AbstractTableModel
中没有此项。行
只需直接使用行
SolutionTableModel
中也有相同的捕获。我更喜欢定义变量self
,该变量指向对象的已创建实例prototype
它应该是prototype
this.prototype
它是create对象的属性prototype
,但是你想要一个构造函数SolutionTableModel
,所以你需要将它分配给SolutionTableModel.prototype
。不要试图在实例上设置原型,在SolutionTableModel函数上设置它。不太了解第一条评论。你能把它作为一个答案吗?比如:)这是最像java的解决方案,对我来说太好了:)同意这里的@Braden,确实很优雅。但是这种方法效率很低。。。您只需使用持久构造函数模式和原型继承之类的东西,每当您调用子类型构造函数时,您都会调用Object.create()方法和超类型构造函数,并创建类似独立对象的东西,而不是从超类型实例继承有多低效?如果有人在使用我的程序时,我需要使用这个构造函数3次,那么这是一个很大的问题吗?
function AbstractTableModel(rows) {
this.rows = rows;
}
AbstractTableModel.prototype.showRows = function() {
console.log('rows ' + this.rows);
}
function SolutionTableModel(rows) {
AbstractTableModel.call(this, rows);
this.show = function() {
this.showRows();
};
}
SolutionTableModel.prototype = Object.create(AbstractTableModel.prototype);
var stm = new SolutionTableModel(3);
stm.show();
function AbstractTableModel(rows) {
this.showRows = function () {
alert('rows ' + rows);
}
}
function SolutionTableModel(rows) {
var self = this;
this.prototype = new AbstractTableModel(rows);
this.show = function () {
self.prototype.showRows();
};
}
var stm = new SolutionTableModel(3);
stm.show();