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 p开发者_JS百科arameters you should pass? If I omit index parameter, I'll get an error... Why do I must pass 3 parameters? What's the catch?
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.
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.
精彩评论