jquery - jQuery 对象上的函数重载

标签 jquery overloading

我正在尝试向 jQuery 对象添加一个与另一个方法同名(但参数集不同)的方法。

到目前为止我所得到的:

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

但是,此代码(特别是 this.insertBefore(where); 行)调用同一函数,而不是 jQuery insertBefore()功能,根据需要。我需要做什么才能将此函数添加到 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);

最佳答案

覆盖之前请备份原始函数。像这样的事情:

(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);

关于jquery - jQuery 对象上的函数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8646609/

相关文章:

java - 覆盖 java...Object 类中的 equals 方法

javascript - jQuery(附加、分离、重新附加)事件处理程序问题

javascript - 按类别中的属性对 div 进行排序

javascript - 购物车 block 未更新 codeigniter

javascript - 从内容内部着色 td

.net - Graphics.DrawRectangle(Pen,RectangleF)

javascript - 空表单字段周围的 jQuery/Javascript 边框

java - 为什么重载 main 方法会出现语法错误?

c++ - 函数重载和方法重载的区别

java - 强制调用父类中的特定构造函数