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

时间:2012-07-04 13:15:42

标签: javascript extjs extjs4 extjs-mvc extjs4.1

我有以下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');
},

问题:当前代码多余:handlerAhandlerB只需使用不同参数调用commonFunc

问题: 我想删除handlerAhandlerB,而不是callapply公共函数commonFunchandlerA和{{handlerB内的任意参数1}}上面的函数,用于不同的事件处理程序。有可能吗?

示例:

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

非常感谢!

1 个答案:

答案 0 :(得分:5)

怎么样:

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');
            }
        }
    });
},
相关问题