与Stapes.js的继承

Syl*_*Syl 2 javascript inheritance stapes.js

我正在尝试使用JS框架Stapes.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');
Run Code Online (Sandbox Code Playgroud)

小提琴这里.

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

Hus*_*sky 5

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

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

var Child = Parent.subclass({
    constructor : function() {
        Parent.prototype.constructor.apply(this, arguments);
    }
});
Run Code Online (Sandbox Code Playgroud)

或这个:

var Child = Parent.subclass({
    constructor : function() {
        Child.parent.constructor.apply(this, arguments);
    } 
});
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,做一个

var child = new Child();
alert(child.name);
Run Code Online (Sandbox Code Playgroud)

将给出一个带有'syl'的警告框