我从互联网上找到的某本书中获取了这段代码...
sm: new Ext.grid.RowSelectionModel({
singleSelect: true,
listeners: {
rowselect: {
fn: function(sm,index,record) {
Ext.Msg.alert('You Selected',record.data.title);
}
}
}
});
现在,sm
是选择模型的简写,我们在这里讨论 ExtJS GridPanel...直到 fn:
部分,一切都清楚了。 AFAIK,匿名函数传递了 3 个参数:sm、index 和 record。
现在我即将因为问一些极其琐碎的问题而投票:你怎么知道应该传递哪些参数?如果我省略索引参数,我会收到错误...为什么我必须传递3个参数?有什么问题吗?
最佳答案
考虑这种情况:
//called with (selectionModelInstance, Index, Record)
function myCallback(sm,index,record) {
//Parameter mapping:
//sm -> selectionModelInstance
//index -> Index
//record -> Record
alert(record.data);
//record is actually a record object, so record.data works
}
观察跳过参数时会发生什么:
//called with (selectionModelInstance, Index, Record)
function myCallback(sm,record) {
//Parameter mapping:
//sm -> selectionModelInstance
//record -> Index
alert(record.data); //Error
//record is actually Index here, and it obviosly doesn't have data property.
}
您看到的错误与调用函数时参数不匹配无关。 Javascript 允许使用任意数量的参数调用任何带有任意数量参数的函数。该错误与尝试取消引用不存在的属性 record.data
相关。
要回答你的问题,你必须使用API指定的签名来定义回调函数,只是为了正确映射参数。
关于javascript - ExtJS监听器: anonymous function parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3401502/