ExtJS 监听器:匿名函数参数

发布于 2024-09-12 17:09:20 字数 518 浏览 6 评论 0原文

我从互联网上遇到的某本书中获取了这段代码...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-09-19 17:09:20

考虑这种情况:

//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​​指定的签名来定义回调函数,只是为了正确映射参数。

Consider this scenario:

//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
}

Watch what happens when you skip a parameter:

//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.
}

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.

瞎闹 2024-09-19 17:09:20

参数不按名称传递;它们通过“位置”传递(就像大多数其他脚本和编程语言一样)。回调必须具有三个参数,因为调用者将提供三个参数;如果不匹配,就会出现错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文