Code


    $(document).ready(function() {
        var obj = {
            url: "http://jquery.com/",
            success: function() {
                this.helper();
                output.add("Hurrah!");
            },
            failure: function() {
                this.helper();
                output.add("Boohoo!");
            },
            helper: function() {
                // do something here
            }
        };

        var myInstrumentation = function(option) {
            if ($.isFunction(option)) {
                return function() {
                    if (!this.count) this.count = 0;
                    this.count++;
                    option.call(this);
                }
            } else {
                return option;
            }
        };

        var newObj = $.extend( {}, obj)
        $.each(["success", "failure"], function() {
            if (obj[this]) newObj[this] = myInstrumentation(obj[this]);
        });

        newObj.success.call(newObj);
        newObj.failure.call(newObj);
        newObj.success.call(newObj);
    });

Output

Explanation

In a thread on the jQuery development mailing list gMinuses suggest that the jQuery map function be extended to work on arbitrary objects.

In the simpler case, a straightforward newMap function works fine. If the object is just slightly more complicated, though, I believe, things start to fall apart. The approach here solves the problem of instrumenting such callbacks without extending $.map.