On Apr 12, 2012, at 8:38 PM, Russell Leggett wrote: > > > At first the answer to this didn't really matter to me, because how often > does someone pass undefined to a function like foo(undefined). I know I > don't, though I'm sure it happens occasionally. Then I thought about it and > realized that it happens in my code all the time, just not like that. A much > more common case is a pass through of an argument to another function. > > function fadeIn(duration=200){...} > function fadeOut(duration=200){...} > function fadeToggle(duration){ > if(visible){ > fadeOut(duration); > }else{ > fadeIn(duration); > } > } > > Here, the argument duration is always passed through fadeToggle to fadeIn or > fadeOut. Someone writing fadeIn would always expect to have a default of 200. > fadeToggle does not care about the duration so much as it wants to pass it on > and use the defaults of the functions it calls. If passing undefined does not > trigger the default, it would have to be rewritten like: > > function fadeToggle(duration){ > var hasDuration = typeof duration != "undefined"; > if(visible){ > if(hasDuration){ > fadeOut(duration); > }else{ > fadeOut(); > } > }else{ > if(hasDuration){ > fadeIn(duration); > }else{ > fadeIn(); > } > } > }
I'd write it: function fadeToggle(...args){ if(visible){ fadeOut(...args); }else{ fadeIn(...args); } } If you don't care about the the actual argument values are just passing them on that's how you should do it. Allen
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss