Overriding jQuery.find () function to provide additional functionality

I want to override jQuery.find to override additional functions. I used the idea discussed on the Ben Nadel blog to do this ( http://www.bennadel.com/blog/1624-Ask-Ben-Overriding-Core-jQuery-Methods.htm ), but for some reason it not working for $ .find (), this is my code

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

        // Define overriding method.
        jQuery.find = function () {
            // Log that we are calling the overridden function.
            Console.log("------> Find method called");

            // then execute the original method
            var results = originalFindFunction.apply(this, arguments);
            return results;
        };
    })();

Do you have an idea what is wrong or how can I override the jquery function?

+3
source share
3 answers

The demo in your link uses a function from .fn.

jQuery.fn.remove

Where do you redefine the function to

jQuery.find

this. .fn. - , "" .

this , . , "".

+1

/ jQuery.fn.find jQuery.find.


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

    // Define overriding method.
    jQuery.fn.find = function () {
        // Log that we are calling the overridden function.
        Console.log("------> Find method called");

        // then execute the original method
        var results = originalFindFunction.apply(this, arguments);
        return results;
    };
})();
+1

. , jQuery ( "fn" ), $.find. , :

var oldFind = $.find;

$.find = function() {
    console.log("something new.");

    console.log(oldFind.apply(this, arguments));
};

$.find("div");

, . :

(function() {...})();

This makes the code in the first couple, self-executing (correct me if I am wrong here), which may not have the desired intention if you call the contained code outside this function. The following is a working example of the snippet below: http://jsfiddle.net/nuPLV/

0
source

All Articles