Let's assume that you would NOT return the same value you are passing... check this one out.
function name(opt) {
return {
'cat' : 'Tom',
'dog' : 'Spike'
}[opt];
}
Cute trick, no? Even returns undefined on no match -- Though in practice making the object a global or scope restricted BEFORE the function would be more efficient the more times you call name().
Creating it as a property of the object is cute:
function name(opt) {
// sadly we can't "this" here, lifting is good, it's not THAT good
return name.names[opt];
}
name.names = {
'cat' : 'Tom',
'dog' : 'Spike'
};
But at that point you might as well just:
var names = {
'cat' : 'Tom',
'dog' : 'Spike'
};
and just do names['cat'] instead of getting 'functions for nothing' involved by doing name('cat'). That's the problem with overdoing the 'functional pattern' nonsense, you often end up making more code than you need and introducing unnecessary overhead.