簡體   English   中英

Ext繼承-無法從基類/抽象類獲取靜態值

[英]Ext inheritance - unable to get a static value from a base/abstract class

我這里有兩個類, ShapeRectangle -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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM