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:
jsfiddle: http://jsfiddle.net/BCwm4/Code:(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'; } });



Reply With Quote




Bookmarks