Add a line of code to ALL functions

后端 未结 3 1872
Happy的楠姐
Happy的楠姐 2020-11-30 15:53

So I am working in JS a lot, and I am working a lot with events (try to stay as modular as possible). Current I am calling Event.fire(\'eventName\') at the end

3条回答
  •  不知归路
    2020-11-30 16:35

    You could try something like this, dynamically modifying your functions:

    var obj = MyClass.prototype;
    for (var prop in obj)
        if (typeof obj[prop] == "function") // maybe also prop != "on" and similar
            (function(name, old) {
                obj[prop] = function() {
                    var res = old.apply(this, arguments);
                    Event.fire(name);
                    return res;
                };
            })(prop, obj[prop]);
    

提交回复
热议问题