[英]Unable to access/get value of abstract (base) class properties from derived class - TypeScript
[英]Ext inheritance - unable to get a static value from a base/abstract class
我這里有兩個類, Shape和Rectangle -Rectangle從Shape繼承。
這是父類:
Ext.define('Shape', {
id: 1,
name: 'ShapeX',
static: {
drawnShapes: ['Shape1', 'shape2'],
getDrawnShapesCount: function () {
console.log('the number of drawn shapes is :'
+ this.drawnShapes.length );
}
},
draw: function (newShape) {
debugger;
var newShape;
this.static.drawnShapes.push(newShape);
console.log( newShape
+ ' is drawn.... \n the number of drawnShapes is '
+ this.static.drawnShapes.length
+ '" \n Shape class defined!' );
}
});
這是我從父類繼承的類:
Ext.define('Rectangle', {
extend: 'Shape',
draw: function (Arrlenght,base.newShape) {
var Arrlenght = inheritableStatics.drawnShapes.length;
alert(Arrlenght); // I need this array value?
if (Arrlenght <= 10) {
this.callParent();
} else {
console.log('Too many shapes drawn!');
}
console.log('Drawing a Rectangle...');
}
});
我的問題是在draw
方法內的Rectangle類中-我想從父類中獲取drawShapes
數組的長度-然后,如果長度<= 10
請調用父類,它將添加新形狀,否則返回消息: “繪制的形狀太多了!” 。
如何引用靜態數組?
首先要犯兩個錯誤:
static
應該是statics
(復數)-不是您需要它,因為... inheritableStatics
是一個配置屬性,而不是運行時訪問器,應在Shape
上定義它。 Ext.define('Shape', {
// ...
inheritableStatics: {
drawnShapes: ['Shape1', 'Shape2']
}
});
然后,如果要引用靜態變量,則可以使用每個對象實例上都存在的self
屬性-本質上是對其原型/類的引用:
Ext.define('Rectangle', {
extend: 'Shape',
// ...
draw: function(/* args */){
console.log(this.self.drawnShapes); // do stuff
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.