function doStuff(params){
params = params || {};
var foo = params.foo || 'fooValue';
var bar = params.bar || 'barValue';
...
}
I love how clean this pattern is. The API is flexible and it reads very easily. Unfortunately, there is a very subtle bug - you can't pass in values that are falsy because || uses type coercion. So this:
function doStuff(foo){
foo = foo || 'foo not specified';
alert( foo );
}
foo( '' );
will give you 'foo not specified' when the intent was to use an empty string. You can only rely on the default operator when falsy values cannot be valid values.
I have begun to use a default hash pattern I first saw in Ruby on Rails code (and is also used extensively in script.aculo.us):
function doStuff(params){
params = params || {};
var default = { foo : 'fooDefault', bar : 'barDefault'};
params = merge( defaults, params );
alert( params.foo );
};
This will give you what you want.
Here here an example merge function:
function merge(ontoObj, fromObj){
for( var i in fromObj ){
if(!fromObj.hasOwnProperty(i))
continue;
ontoObj[i] = fromObj[i];
}
return ontoObj;
}
0 comments: