Posts Tagged ‘tips’

Quick Javascript Tip: Pass an Options Hash

July 13th, 2009

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.