Quick Javascript Tip: Pass an Options Hash

A quick Javascript tip that I pass along in a lot of code reviews is to make use of an options object as a hash to pass a large number of parameters to a function without writing each one individually.  For example:

function doSomething(id, someParameter, anArgument, optionC){ };

vs.

function doSomething(id, options){ };

By encapsulating the 3 additional parameters into an options object and passing them individually, you make for cleaner code.

A quick caveat is to check the options object to ensure that all of the necessary properties exist before running your function.  

About Brian

I'm a UI Software Engineer from Atlanta, GA who now lives in Cambridge, MA. Since late 2009, I've been working as Principal UI Engineer for Brightcove and loving exploring a new part of the US. Previously, I was Sr. UI Engineer for AutoTrader.com. I've been in this business since 1997 and I'm lucky to love what I do.
This entry was posted in Development and tagged , . Bookmark the permalink.

Comments are closed.

blog comments powered by Disqus