Return to Snippet

Revision: 5317
at February 27, 2008 15:20 by 1man


Initial Code
var complex = function(valA, options){
		/**
		 * Set the default values in the object, then extend it to include the
		 * values that we passed to it.
		*/
		var settings = $.extend({
			option1: null,
			option2: null,
			option3: null,
			option4: null
		},options||{});//If no options, pass an empty object
		console.warn(valA);
		console.log(settings.option1);
		console.log(settings.option2);
		console.log(settings.option3);
		console.log(settings.option4);
	};
	complex('Value A', {option1: 'this is option 1'});

Initial URL

                                

Initial Description
If you have a complex function you may need to pass it lots of arguments. Instead of having to remember how many you need to pass, and passing 'null' for un needed arguments, pass the function an object.

Now you can set the defaults inside the function, which will be superseeded if they are passed as an argument.

Initial Title
Option Hash Using jQuery

Initial Tags
jquery

Initial Language
JavaScript