How to override a function in a jQuery plugin and have access to options/variables

I’m working on a plugin that needs to allow for some functions to be swapped out for custom ones. When I do the swap, the function runs, but it needs to have access to the options/variables that are inside the plugin. I’ve tried searching around, but I’m probably searching for the wrong thing. I’d appreciate help if anyone knows what I’m trying to do:


(function($){
    $.fn.test = function( options ){

        /* Set defaults */
        var defaults = {
            some_value: 'a',
            do_something: function(){
                alert('this is the default function');
            }
        };

        /* Override defaults with options */
        var options = $.extend( defaults, options );

        $(this).click(function(){
            console.log( options.do_something() );
        });
    }
})(this.jQuery);

$('span').test({
    do_something: function(){

        // ReferenceError: options is not defined
        alert( options.some_value );

        // What I really want to do is set some_value
        options.some_value = 'b';
    }
});

jsfiddle: http://jsfiddle.net/BCwm4/

The this keyword can be used to access the function, so you could use this.some_value for example.

If I change line 36 to:


alert( this.some_value );

The alert says “undefined”.

When that very same change is made to your jsfiddle code, it works.

See? http://jsfiddle.net/pmw57/BCwm4/2/

I was wrong. My code was slightly different because I was playing with it for so long, I had added some extra stuff. Thanks for your help. Everything is good now!