Holy ECMA Hacks, Batman: the Weirdest Path To Default Arguments Ever
September 13, 2006 12:46 pm ECMA / Javascript, Programming, Web and Web StandardsYeah, so, I’m having a hard time adressing the weirdness that is ParentNode’s approach to default functions. Basically he sets a hook on the prototype for calling the function, and in that he makes a new function which checks for the presence of a particular member (we remember that functions are objects in ECMA, I trust.) If that member exists, he chops it up, and for each argument in the original function call, he tests for undefinedness, and if that’s present, he fills in the value. (This one is order-based default, but there’s also a by-name default.)
Now, I think it’s a little impractical, and it leads to a mind-bleedingly weird syntax, but it’s actually struck me that this is a very clean, very terse format for providing something akin to uniform construction behavior when you’re dealing with multiple manufacture paths to a given object. Granted, it’d be an even bigger, even uglier hack, but still, it’d work.
Witness syntax:
function exampleF(foo, bar, baz) { … }.defaults(1, 3, 7);
Hear that crashing sound? It’s your mind, breaking. But, as much as I hate to admit it, this is one genuinely lovable hack. Bravo.

September 13th, 2006 at 3:59 pm
*brain explodes*