在我目前的项目中,我使用的是ExtJs3.3.
我创建了许多具有私有变量和函数的类.例如:
我创建了许多具有私有变量和函数的类.例如:
MyPanel = function(config){
config = config || {};
var bar = 'bar';//private variable
function getBar(){//public function
return bar;
}
function foo(){
//private function
}
Ext.apply(config,{
title: 'Panel',layout: 'border',id: 'myPanel',closable: 'true',items: []
});
MyPanel.superclass.constructor.call(this,config);
};
Ext.extend(MyPanel,Ext.Panel,{
bar: getBar
});
Ext.reg('MyPanel',MyPanel);
我知道ExtJs4中新的做事方式是使用Ext.define方法.所以我上面的代码看起来像这样:
Ext.define('MyPanel',{
extend: 'Ext.panel.Panel',title: 'Panel',closable: true,constructor: function(config) {
this.callParent(arguments);
},});
所以我想知道的是如何在ExtJs4中定义私有变量和函数,类似于我在ExtJs3中完成的方式?
换句话说,我明白Ext.define方法将负责定义,扩展和注册我的新类,但是我应该在哪里声明一个不是类本身的属性但是该类所需的javascript var.
MyPanel = function(config){
//In my Ext3.3 examples I was able to declare any javascript functions and vars here.
//In what way should I accomplish this in ExtJs4.
var store = new Ext.data.Store();
function foo(){
}
MyPanel.superclass.constructor.call(this,config);
};
解决方法
我不是强制这样的私有变量的大粉丝,但当然可以做到这一点.只需在构造函数/ initComponent函数中为变量设置一个访问器函数(闭包):
constructor: function(config) {
var bar = 4;
this.callParent(arguments);
this.getBar = function() {
return bar;
}
},...