如何覆盖 javascript 中返回的嵌套方法?

how to override a returned nested method in javascript?

假设我正在使用一个代码如下所示的库:

(function($)
{
    function Library(el, options)
    {
        return new Library.prototype.init(el, options);
    }
    Library.fn = $.Library.prototype = {
        init: function(el, options) {
            this.$elm.on('keydown.library', $.proxy(this.keydown.init, this));
        }
        keydown: function() {
            return {
                init: function(e) {
                    ... somecode
                },
                checkStuff: function(arg1, arg2) {
                    ...someCode
                }
            }
        };
    }
})(jQuery);

它有一个插件系统,可以在 Object {init: function, keydown: function...} 中访问它。我想覆盖 keydown.init 函数。通常我可以看到使用 _.wrap 之类的东西来做到这一点:

somefunc = _.wrap(somefuc, function(oldfunc, args) {
     donewstuff();
     oldfunc.call(this.args);
});

但这似乎不适用于返回的嵌套方法,例如:

this.keydown.init = _.wrap(this.keydown.init, function(oldfunc, args) {
     donewstuff();
     oldfunc.call(this.args);
});

这个问题可能会在这里得到解答,但我真的不知道用什么词来描述这种编码风格,所以很难搜索到。如果您让我知道将其称为嵌套返回方法是否正确,可加分?

这种模式称为module。您在这里可以做的最好的事情是缓存您想要覆盖的方法并在您的覆盖中调用缓存的方法:

somefunc._init = somefunc.init;
somefunc.init = function () {
    doStuff();
    this._init();
};

我检查了 _.wrap,它做了同样的事情,正如另一个答案所指出的,您缺少的是您失去了 somefunc 的上下文。为了防止你可以这样做:

somefunc.init = _.wrap(_.bind(somefunc.init, somefunc), function (oldRef, args) {
    doStuff();
    oldRef.call(this.args);
});

问题是你的方法运行断章取意。

您需要设置其 this 上下文(为此使用 .bind()

somefunc.init = _.wrap(somefuc.init.bind(somefunc), function(oldfunc, args) {
     donewstuff();
     oldfunc.call(this.args);
});

您需要修饰(读取:包装)keydown 函数,以便您可以包装对象的 init 方法 returns:

somefunc.keydown = _.wrap(somefunc.keydown, function(orig) {
    var module = orig(); // it doesn't seem to take arguments or rely on `this` context
    module.init = _.wrap(module.init, function(orig, e) {
         donewstuff();
         return orig.call(this, e);
    });
    return module;
});