ExtJS 监听器:匿名函数参数
我从互联网上遇到的某本书中获取了这段代码...
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个参数?有什么问题吗?
I grabbed this code form some book I've bumped on the InternetS...
sm: new Ext.grid.RowSelectionModel({
singleSelect: true,
listeners: {
rowselect: {
fn: function(sm,index,record) {
Ext.Msg.alert('You Selected',record.data.title);
}
}
}
});
now, sm
is shorthand for selection model, we're discussing a ExtJS GridPanel here... Everything is clear until the fn:
part. AFAIK, an anonymous function is passed with 3 parameters: sm, index, and record.
Right now I'm about to get down votes for asking something extremely trivial: how do you know which parameters you should pass? If I omit index parameter, I'll get an error... Why do I must pass 3 parameters? What's the catch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑这种情况:
观察跳过参数时会发生什么:
您看到的错误与调用函数时参数不匹配无关。 Javascript 允许使用任意数量的参数调用任何带有任意数量参数的函数。该错误与尝试取消引用不存在的属性
record.data
有关。为了回答你的问题,你必须使用API指定的签名来定义回调函数,只是为了正确映射参数。
Consider this scenario:
Watch what happens when you skip a parameter:
The error that you seeing has nothing to do with parameter mismatch when calling a function. Javascript allows any function taking any number of parameters to be called with any number of parameters. The error is to do with trying to dereference the property
record.data
which is not there.To answer you question, you must define the callback function using the signature specified by the API, simply for the sake of parameters being mapped correctly.
参数不按名称传递;它们通过“位置”传递(就像大多数其他脚本和编程语言一样)。回调必须具有三个参数,因为调用者将提供三个参数;如果不匹配,就会出现错误。
Parameters are not passed by name; they are passed by "position" (like in most other scripting and programming languages). The callback has to have three parameters, because the caller will provide three arguments; if there's a mismatch, an error will occur.