2013-03-21 51 views
2

我想與JS框架Stapes.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'); 

小提琴here

如何從子類訪問父項的名稱屬性?

回答

5

對於那些懶惰點擊鏈接,下面是完整的答案,我給在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