我这里有两个类, 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
则调用将添加新形状的父级,否则返回消息:&#34;绘制的形状太多!&#34; 。
如何引用静态数组?
答案 0 :(得分:2)
首先出现两个错误:
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
}
});