我的Javascript继承方法有什么问题?

时间:2013-06-27 15:58:51

标签: javascript

我知道有很多方法可以进行JS继承。我想在这里做一些事情,我将传递到我的子类中,我想在构造函数时将它传递给超类:

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();
小提琴:

http://jsfiddle.net/YuqPX/2/

它似乎没有用,因为原型方法没有流下来:(任何想法?

4 个答案:

答案 0 :(得分:4)

Live Demo

首先,您必须定义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();

答案 1 :(得分:2)

function AbstractTableModel(rows) {
  this.rows = rows;
  this.showRows = function() {
    alert('rows ' + this.rows);
  }
}

function SolutionTableModel(rows) {
  var soln = Object.create(new AbstractTableModel(rows));
  soln.show = function() {
    this.showRows();
  };
  return soln;
}
var solution = new SolutionTableModel(5);
solution.show();

这是进行对象继承的一种方法。在我看来,这种方法最为优雅,可以在here

中找到

Fiddle

答案 2 :(得分:1)

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();

答案 3 :(得分:1)

这是一个基于您所做的工作示例 DEMO

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();
  1. 在您的班级AbstractTableModel中,没有this.rows直接使用rows
  2. 第二课SolutionTableModel中的同一个问题。我更喜欢定义指向所创建的对象实例的变量self
  3. 您错过了protoype类型prototype