ExtJS:在哪里定义基类的原型扩展?

时间:2013-09-17 16:05:38

标签: javascript extjs extjs4

假设我想通过向各自原型添加功能来扩展NumberString

我在哪里将代码放在ExtJS 4应用程序中,以便它可以在任何地方使用?

我已尝试将所有Number.prototype.<whatever>放入单独的文件app.prototypes中,然后将此文件附加到我的requires配置中,用于Ext.application ...但是然后应用程序无声地失败(不加载,没有错误)。

么?

这就是我所拥有的...但它不起作用:

Ext.define('PT.overrides.Number', {
override: 'Ext.Number',

constructor:function(config) {
    var me = this;

    me.callParent(arguments);

    me.formatMoney = function(c, d, t) {
        console.log('called');              // never called
        var result;
        // blah, blah
        return result;
    }
}
});

2 个答案:

答案 0 :(得分:1)

不要扩充基础原型,这是目前在所有主要JavaScript框架中避开的糟糕风格。要么在像Trendy建议的新类中扩展Ext.Number,要么只使用Ext.util.Format方法 - 不需要在这里重新发明轮子。

答案 1 :(得分:0)

您可以使用Ext Define来定义/扩展类:

Ext.define('Your.Class.Name' /*null for anonymous class*/, {

     extend:'Ext.Number' /*class to extend*/,

     constructor:function(config) {

          this.initConfig(config);

          return this;
     }
});