扩展现有jQuery函数

扩展现有jQuery函数,jquery,function,extend,Jquery,Function,Extend,我正在尝试编写一个插件,它将扩展jQuery中的现有函数,例如 (function($) { $.fn.css = function() { // stuff I will be extending // that doesn't affect/change // the way .css() works }; })(jQuery); 我只需要扩展.css()函数的几个位。请注意,我在考虑PHP类,因为您可以扩展现有类,所

我正在尝试编写一个插件,它将扩展jQuery中的现有函数,例如

(function($)
{
    $.fn.css = function()
    {
        // stuff I will be extending
        // that doesn't affect/change
        // the way .css() works
    };
})(jQuery);

我只需要扩展
.css()
函数的几个位。请注意,我在考虑PHP类,因为您可以扩展现有类,所以我想问是否可以扩展jQuery函数。

当然。。。只需保存对现有函数的引用,并调用它:

(function($)
{
    // maintain a reference to the existing function
    var oldcss = $.fn.css;
    // ...before overwriting the jQuery extension point
    $.fn.css = function()
    {
        // original behavior - use function.apply to preserve context
        var ret = oldcss.apply(this, arguments);

        // stuff I will be extending
        // that doesn't affect/change
        // the way .css() works

        // preserve return value (probably the jQuery object...)
        return ret;
    };
})(jQuery);

方法相同,但与此问题的最佳答案略有不同:

// Maintain a reference to the existing function
const oldShow = jQuery.fn.show

jQuery.fn.show = function() {
  // Original behavior - use function.apply to preserve context
  const ret = oldShow.apply(this, arguments)

  // Your source code
  this.removeClass('hidden')

  return ret
}

你会补充什么?