簡體   English   中英

設置JavaScript以調用具有不同參數的函數

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

問題:當前代碼是多余: handlerAhandlerB只是調用commonFunc用不同的參數

問:我想刪除handlerAhandlerB ,而是callapply的共同作用commonFunc與內任意參數handlerAhandlerB功能之上,針對不同的事件處理程序。 可能嗎?

例:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM