Ext 无法从基/抽象类中获取静态值

Ext unable to get a static value from a base/abstract class

本文关键字:获取 静态 抽象类 Ext      更新时间:2023-09-26

我这里有两个类,形状矩形 - 其中矩形继承自形状。
下面是父类:

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
    }
});