简体   繁体   English

设置JavaScript以调用具有不同参数的函数

[英]Set up JavaScript to call function with different parameters

I have the following ExtJS controller code: 我有以下ExtJS控制器代码:

init: function() {
    this.control({
        '#menuitem-A': { click: this.handlerA },
        '#menuitem-B': { click: this.handlerB },
    });
},

and the following event handlers: 和以下事件处理程序:

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');
},

Problem: the current code is redundant: handlerA and handlerB just call commonFunc with different parameters 问题:当前代码是多余: handlerAhandlerB只是调用commonFunc用不同的参数

Question: I would like to remove handlerA and handlerB and instead call or apply the common function commonFunc with the arbitrary parameters within handlerA and handlerB functions above, for different event handlers. 问:我想删除handlerAhandlerB ,而是callapply的共同作用commonFunc与内任意参数handlerAhandlerB功能之上,针对不同的事件处理程序。 Is it possible? 可能吗?

Example: 例:

init: function() {
    this.control({
        '#menuitem-A': { click: /*commonFunc with ['param-A1', 'param-A2']*/ },
        '#menuitem-B': { click: /*commonFunc with ['param-B1', 'param-B2']*/ },
    });
},

Many thanks! 非常感谢!

how about this: 这个怎么样:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM