扩展现有的jQuery函数

Mac*_*Mac 39 jquery function extend

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

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

我只需要几个位来扩展该.css()功能.请注意,我可以考虑PHP类className extend existingClass,所以我想问是否可以扩展jQuery函数.

小智 78

当然......只需保存对现有功能的引用,并调用它:

(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);
Run Code Online (Sandbox Code Playgroud)