![](/img/trans.png)
[英]How to set default values for parameters of a JavaScript function and call it with less parameters?
[英]Set up JavaScript to call function with different parameters
我有以下ExtJS控制器代码:
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
,而是call
或apply
的共同作用commonFunc
与内任意参数handlerA
和handlerB
功能之上,针对不同的事件处理程序。 可能吗?
例:
init: function() {
this.control({
'#menuitem-A': { click: /*commonFunc with ['param-A1', 'param-A2']*/ },
'#menuitem-B': { click: /*commonFunc with ['param-B1', 'param-B2']*/ },
});
},
非常感谢!
这个怎么样:
init: function() {
this.control({
'#menuitem-A': {
click: function(button, event){
this.commonFunc('param-A1', 'param-A2');
}
},
'#menuitem-B': {
click: function(button, event){
this.commonFunc('param-B1', 'param-B2');
}
}
});
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.