![](/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.