scope : this 问题

[code="js"]GridPanel = Ext.extend(Ext.grid.GridPanel, {
_store : null,
// 构造方法
constructor : function(_config) {
if (_config == null) {
_config = null;
}
Ext.apply(this, _config);
this._store = new Ext.data.JsonStore({
data : [{
name : "张三",
sex : "男",
age : 11
}, {
name : "李四",
sex : "女",
age : 10
}],
fields : ["name", "sex", "age"]
});
// 拷贝父类的构造方法到当前对象
ViewForm.superclass.constructor.call(this, {
width : 300,
autoHeight : true,
store : this._store,
renderTo : Ext.getBody(),
// 取消标题下拉选项
enableHdMenu : false,
// 禁止拖动
enableColumnMove : false,
// 定义单选 selModel可简写sm
sm : new Ext.grid.RowSelectionModel({
singleSelect : true,
listeners : {
"rowselect" : {
fn : function(_sm, _index,
_data) {
this.fireEvent("rowse",
_data);
},
scope : this
}
}
}),
// colModel 可简写cm
cm : new Ext.grid.ColumnModel([{
header : "姓名",
align : "center",
sortable : true,
dataIndex : "name"
}, {
header : "性别",
align : "center",
sortable : true,
dataIndex : "sex"
}, {
header : "年龄",
align : "center",
sortable : true,
dataIndex : "age"
}])
});
this.addEvents("rowse");
}
});
[/code]
以下是调用
[code="js"]
Ext.onReady(function() {
var _grid = new GridPanel();
_grid.on("rowse", function(_data) {
_form.loadRecord(_data);
});
});
[/code]

如果代码没scope : this,则_grid.on无法监听

请解答下为什么.3Q

[code="js"]function(_sm, _index, _data) {

this.fireEvent("rowse", _data);

}[/code]
如果只看上面一段,你就会知道this是指这个function的内部作用域。
那么这个域内部有fireEvent方法吗?当然没有。

Extjs的scope:this是将上面的function内this作用域改变为整个控件的作用域。
至于怎么实现的,你可以参考源码实现。其本质就是call与apply的调用。

[table]
| fn : function(_sm, _index,

|
| _data) { |
[/table]