我想删除代码中的函数创建,但是我不知道怎么办?这里有人可以帮助我吗?

    for (var eventName in this.events) {
        var eventFunc = this.events[eventName],
            splitEventName = eventName.split(" "),
            eventType = splitEventName[0],
            eventUI = splitEventName[1];

        Utilities.addEventListener(this.ui[eventUI], eventType, (function(view, eventFunc) {
            return function(e) {
                eventFunc.call(view, e);
            };
        }(this, eventFunc)));
    }

最佳答案

您可以将立即调用的函数表达式移出循环,然后将其转换为常规函数声明。然后,您可以在每次迭代中简单地调用它:

function makeEventListener(view, eventFunc) {
    return function (e) {
        eventFunc.call(view, e);
    };
}
for (var eventName in this.events) {
    var eventFunc = this.events[eventName],
        splitEventName = eventName.split(" "),
        eventType = splitEventName[0],
        eventUI = splitEventName[1];
    Utilities.addEventListener(this.ui[eventUI], eventType, makeEventListener(this, eventFunc));
}

07-24 17:10