Javascript 镫骨遗传

Javascript 镫骨遗传,javascript,inheritance,stapes.js,Javascript,Inheritance,Stapes.js,我正在尝试与JS框架建立父/子链接 这是我的密码: var Parent = Stapes.subclass({ constructor: function () { this.name = 'syl'; } }); var Child = Parent.subclass({ constructor: function (value) { this.value = value; console.log(this.name

我正在尝试与JS框架建立父/子链接

这是我的密码:

var Parent = Stapes.subclass({
    constructor: function () {
        this.name = 'syl';
    }
});

var Child = Parent.subclass({
    constructor: function (value) {
        this.value = value;

        console.log(this.name); // undefined
    }
});

var child = new Child('a value');
小提琴


如何从子类访问父类的name属性

问题已解决,请参阅。

对于那些懒得点击链接的人,以下是我在Github上给出的完整答案:

子类不会自动运行其父类的构造函数。您需要手动运行它。您可以这样做:

var Child = Parent.subclass({
    constructor : function() {
        Parent.prototype.constructor.apply(this, arguments);
    }
});
或者这个:

var Child = Parent.subclass({
    constructor : function() {
        Child.parent.constructor.apply(this, arguments);
    } 
});
在这两种情况下,执行

var child = new Child();
alert(child.name);
将给出一个带有“syl”的alertbox