3
我有以下ExtJS的控制器代码:设置JavaScript来调用函数使用不同的参数
init: function() {
this.control({
'#menuitem-A': { click: this.handlerA },
'#menuitem-B': { click: this.handlerB },
});
},
和下面的事件处理程序:
commonFunc: function(param1, param2) {
// do something with param1 and param2 in here
},
handlerA: function(button, event) {
this.commonFunc('param-A1', 'param-A2');
},
handlerB: function(button, event) {
this.commonFunc('param-B1', 'param-B2');
},
问题:当前代码是冗余的: handlerA
和handlerB
只需拨打commonFunc
不同的参数
问题: 我想内handlerA
和handlerB
功能与任意参数以除去handlerA
和handlerB
和代替call
或apply
公共功能commonFunc
以上,对于不同的事件处理程序。可能吗?
例子:
init: function() {
this.control({
'#menuitem-A': { click: /*commonFunc with ['param-A1', 'param-A2']*/ },
'#menuitem-B': { click: /*commonFunc with ['param-B1', 'param-B2']*/ },
});
},
非常感谢!
因此,它是不可能建立的函数(当事件触发),而无需实际建立一个中间函数的调用? –
你可以使用Ext.Function.bind(),但它本质上只是做同样事情的捷径。上面提供的答案在功能上与您的答案没有区别。 –
@EvanTrimboli试过了,它的工作原理,谢谢! –