Rozszerzenie istniejącej funkcji jQuery

Próbuję napisać wtyczkę, która rozszerzy istniejącą funkcję w jQuery, np.

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

Do rozszerzenia funkcji .css() jest tylko kilka bitów. Przepraszam za pytanie, myślałem o klasach PHP, ponieważ można className extend existingClass, więc pytam, czy jest możliwe rozszerzenie funkcji jQuery.

Author: MacMac, 2011-02-15

2 answers

Jasne... Po prostu zapisz odwołanie do istniejącej funkcji i wywołaj ją:

(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);
 81
Author: Test Employee 1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-29 13:12:49

Tak samo, ale trochę inaczej niż najlepsza odpowiedź na to pytanie:

// 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
}
 0
Author: Kevin Campion,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-11-16 07:58:56