I have another JavaScript style question, in part prompted by Bear Bibeault's
clarity vs. cleverness meter.
Some background: I sometimes find myself exhorting some of my colleagues to "write JavaScript that looks like JavaScript" (as opposed JavaScript that looks some other language X). In other words, let's go ahead and take advantage of some of JavaScript's distinctive features. (And as a team, let's grow and share our knowledge.) I sometimes face Crockfordian protests of "excessive cleverness" (or just "the old, familiar way is clearer to everyone, not just JavaScript gurus, so let's continue with the style we know and love.")
(1)
So somebody had a switch statement going that had about a dozen cases but the point was just to map one
string value to another. So I proposed replacing it with a plain-old JavaScript object, serving as a hash/map/dictionary thing.
So everyone's good with that. But then I start to get a little crazy. In this particular case "lookup" is used exactly once. So to my mind, why bother giving it a name. Let's do it like this:
And in fact, "input" isn't entirely needed either and the code could be further tightened up. (Rest assured, there is appropriate null-checking, input-scrubbing, etc. in the code, so my proposed changes don't introduce any faults here.)
So, is this now JavaScript that looks like JavaScript (and an idiom every reasonably good JavaScript programmer should be comfortable with) or is it the bad kind of cleverness?
(2)
That was a relatively concrete example, and one that straightfowardly maps a string to a string. What about mapping a string to behavior? (Mit bonus IIFE!)
Now, is this JavaScript that looks like JavaScript? Bad kind of clever? In general, good or bad idea?