Ext.define 初始化对象属性回调
Ext.define init object property on callback
我的例子:
Ext.define('Shpak',
{
name: 'Eugene',
surname : 'Popov',
getParam:function(){
console.log( this.param + '_____' + this.surname )
}
}
, function(){
console.log('callback')
this.param = 'default';
});
var bird = Ext.create('Shpak')
bird.getParam()//undefined_____Popov
为什么未定义?
检查 Ext.define
的回调函数定义
createdFn : Function (optional) Callback to execute after the class is
created, the execution scope of which (this) will be the newly created
class itself.
回调函数中的this是class而不是对象。
回调函数在class创建后调用,而不是对象。
尝试在构造函数中添加初始化逻辑。检查下面的代码。
constructor: function(cfg) {
this.param = 'default';
this.initConfig(cfg);
}
JS Fiddle: http://jsfiddle.net/n1czm24o/3/
更新:
或将逻辑提取到初始化函数
constructor: function(cfg) {
this.initFields();
this.initConfig(cfg);
},
initFields: function() {
this.param = 'default';
}
JS Fiddle: http://jsfiddle.net/sod1ft49/1/
我的例子:
Ext.define('Shpak',
{
name: 'Eugene',
surname : 'Popov',
getParam:function(){
console.log( this.param + '_____' + this.surname )
}
}
, function(){
console.log('callback')
this.param = 'default';
});
var bird = Ext.create('Shpak')
bird.getParam()//undefined_____Popov
为什么未定义?
检查 Ext.define
的回调函数定义createdFn : Function (optional) Callback to execute after the class is created, the execution scope of which (this) will be the newly created class itself.
回调函数中的this是class而不是对象。
回调函数在class创建后调用,而不是对象。
尝试在构造函数中添加初始化逻辑。检查下面的代码。
constructor: function(cfg) {
this.param = 'default';
this.initConfig(cfg);
}
JS Fiddle: http://jsfiddle.net/n1czm24o/3/
更新:
或将逻辑提取到初始化函数
constructor: function(cfg) {
this.initFields();
this.initConfig(cfg);
},
initFields: function() {
this.param = 'default';
}
JS Fiddle: http://jsfiddle.net/sod1ft49/1/