函数重载jQuery对象

Pat*_*ini 4 jquery overloading

我正在尝试将方法添加到具有相同名称(但不同的参数集)的jQuery对象作为另一种方法.

到目前为止我得到了什么:

jQuery.fn.insertBefore = function(elem, duration)
{
    this.css("display", "none");
    this.insertBefore(elem);
    this.toggle(duration);
}
Run Code Online (Sandbox Code Playgroud)

但是,此代码(特别是this.insertBefore(where);行)根据需要调用此函数,而不是jQueryinsertBefore()函数.为了将此函数添加到jQuery对象,并使其重载(不覆盖)现有函数,我需要做什么?

编辑:解决方案

(function ($)
{
    var oldInsertBefore = $.fn.insertBefore;
    jQuery.fn.insertBefore = function(elem, duration)
    {
        if (duration === undefined)
        {
            oldInsertBefore.call(this, elem);
            return;
        }

        this.css("display", "none");
        this.insertBefore(elem);
        this.toggle(duration);
    }
})(jQuery);
Run Code Online (Sandbox Code Playgroud)

Ser*_*sev 5

在覆盖之前备份原始功能.像这样的东西:

(function($){
    var oldInsertBefore = $.fn.insertBefore;
    jQuery.fn.insertBefore = function(elem, duration)
    {
        oldInsertBefore.apply(this, arguments);
        this.css("display", "none");
        this.insertBefore(elem);
        this.toggle(duration);
    }
})(jQuery);
Run Code Online (Sandbox Code Playgroud)