Estoy tratando de escribir un complemento que amplíe una función existente en jQuery, por ejemplo
(function($) { $.fn.css = function() { // stuff I will be extending // that doesn't affect/change // the way .css() works }; })(jQuery); Solo necesito extender unos pocos bits de la función .css() . Me importa que pregunte, estaba pensando en las clases de PHP, ya que puede className extend existingClass , por lo que pregunto si es posible extender las funciones de jQuery.
Claro... Simplemente guarde una referencia a la función existente y llámela:
(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);De la misma manera pero un poco diferente a la mejor respuesta de esta pregunta:
// 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 }