Thursday, June 20, 2013

Overriding Core jQuery Methods

jQuery is such a well thought out, powerful Javascript library, that it actually uses itself to build itself. What I mean by that is that many of the methods provided by the jQuery library are actually built internally as plugins to the jQuery architecture. When you start to think about the methods in this way, it is a small leap to see that we can override a given jQuery method by simply creating a new plugin of the same name.

(function () {
    // Store a reference to the original remove method.
    var originalRemoveMethod = jQuery.fn.remove;

    // Define overriding method.
    jQuery.fn.remove = function () {
        // Log the fact that we are calling our override.
        console.log("Override method");

        // Execute the original method.
        originalRemoveMethod.apply(this, arguments);
    }
})();

No comments:

Post a Comment