Estoy intentando escribir un complemento que extenderá 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 ampliar algunos bits de la función .css()
. Cuidado con mi pregunta, estaba pensando en las clases PHP ya que puedes className extend existingClass
, entonces estoy preguntando si es posible extender las funciones de jQuery.
Claro … Solo guarda una referencia a la función existente, y llámala:
(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);
Peter Errikson escribe un código muy útil para extender jQuery con dos nuevos eventos, onShow y onHide con muestra de código jsfiddle …
sugiero leer ese artículo … se ve muy bien 🙂