Working on my first jQuery plugin I needed a way for anyone calling the plugin to set some options. I am sure you have seen this in other plugins you have used before. With those options the user can provide you also need to account for no options being set. We can get around this by setting some defaults for configuration options. A user provided options to our plugin No options provided.
In our plugin we will allow the user to pass in the options. Next we need to setup some defaults in case no options are passed to our plugin. Finally we need account for the case where some options are provided but not all of them are. We can do this using the extend function of jQuery. Here we are supplying 2 arguments our options object and our defaults object. This will merge the 2 objects by modifying the first. This way if 0 arguments are passed it will use the defaults but any argument passed in will override our default.
This is a great way to provide a configurable plugin. Please feel free to provide feedback, still a newbie when it comes to jQuery!

#1 by Dan G. Switzer, II on 2/15/10 - 11:14 AM
One thing you'll want to watch when using $.extend(). In your use case, it's actually going to end up altering the "defaults" object. This could cause you problems if you went to ever compare the current value to the default value.
The safer option is to use:
var options = $.extend({}, defaults,options);
This starts with an empty object, appends the defaults and then alters based upon the options. This will make sure the defaults object is never changed.
#2 by Lola LB on 2/22/10 - 6:47 AM
#3 by Dan Vega on 2/22/10 - 7:45 AM
#4 by Lola LB on 3/2/10 - 10:02 AM