日々精進

新しく学んだことを書き留めていきます

Class Systemその4

3. Statics
staticsプロパティでstaticメンバを定義できるよ。

Ext.define('Computer', {
    statics: {
        instanceCount: 0,
        factory: function(brand) {
            //staticメソッド内でのthisはクラスそのものを指すよ
            return new this({brand: brand});
        }
    },

    config: {
        brand: null
    },

    constructor: function(config) {
        //急に出てきたinitConfigが気になるけどこれは引数をconfigに代入するだけのメソッドだろうな。
        this.initConfig(config);

        //selfはクラスを指すよ。
        this.self.instanceCount ++;

        return this;
    }
});

var dellComputer = Computer.factory('Dell');
var appleComputer = Computer.factory('Mac');

alert(appleComputer.getBrand()); // using the auto-generated getter to get the value of a config property. Alerts "Mac"

alert(Computer.instanceCount); // Alerts "2"



IV. Errors Handling & Debugging
Ext.getDisplayName()を使うとプロパティ一覧が出るのでデバッグがラクにになるよ。

throw new Error('['+ Ext.getDisplayName(arguments.callee) +'] Some message here');

Ext.define()で定義されたクラスのオブジェクトはエラー発生時にコールスタックを吐く。Webkit系ブラウザでないと見れないので注意