Javascript继承:一个子类获取原型,另一个子类不';T

Javascript继承:一个子类获取原型,另一个子类不';T,javascript,oop,inheritance,prototype,Javascript,Oop,Inheritance,Prototype,我有一个超类“类”,这个类将由子类和子类b继承(通过原型链)。但是,虽然继承似乎适用于子类a,但对于子类b,继承失败。代码如下: function SuperClass(childCell){ this.childCell = childCell; this.children = new Array(9); for(i=0; i<9; i++) { this.children[i] = new this.childCell(); } } fu

我有一个
超类
“类”,这个类将由
子类
子类b
继承(通过原型链)。但是,虽然继承似乎适用于
子类a
,但对于
子类b
,继承失败。代码如下:

function SuperClass(childCell){
    this.childCell = childCell;
    this.children = new Array(9);
    for(i=0; i<9; i++) {
        this.children[i] = new this.childCell();
    }
}
function SubClassA(){
    this.num = 1;
}
SubClassA.prototype = new SuperClass(SubClassB);
function SubClassB(){
    this.num = 2;
}
SubClassB.prototype = new SuperClass(SubClassC);
function SubClassC(){
    this.num = 3;
}
var x = new SubClassA();
函数超类(childCell){
this.childCell=childCell;
this.children=新数组(9);

对于(i=0;i我通常不会深入挖掘那么多。但是当我们在javascript中使用子分类时,遵循以下模式

function Superclass() { }
Superclass.prototype.someFunc = function() { };

function Subclass() { }
Subclass.prototype = new Superclass();
Subclass.prototype.anotherFunc = function() { };

var obj = new Subclass();

尝试重新排序您的声明示例,如

function Parent(childCell){
    this.childCell = childCell;
    this.children = new Array(9);
    for(var i=0; i<9; i++) {
        this.children[i] = new this.childCell();
    }
}
function ChildA(){
    this.num = 1;
}
function ChildB(){
    this.num = 2;
}
function ChildC(){
    this.num = 3;
}

ChildB.prototype = new Parent(ChildC);
ChildA.prototype = new Parent(ChildB);

嗯……这对我的情况有什么帮助?我相信我有这里写的东西。谢谢!这很好。你能解释一下为什么我必须先定义ChildB原型吗?@Bagavatu试着看看我更新的答案,我希望现在更清楚了
function A() {this.cell = 10}
function B() {this.num =1}

var b1 = new B(); // b1 = {num:1}

B.prototype = new A();
var b2 = new B(); // b1 = {num:1}, b2 = {num:1, cell:10}