undefined being treated as a missing optional argument
dherman at mozilla.com
Fri Apr 13 12:00:13 PDT 2012
On Apr 13, 2012, at 11:51 AM, Allen Wirfs-Brock wrote:
> both sides of this debate seem a little smelly. But, Pretending that undefined isn't a real value seems more smelly to me.
There's no need to think of it as pretending it's not a real value. Think of it as saying that the undefined value is the idiomatic way in JS to represent "no value for the expected type." The smelly thing then is to create API's that both use defaults and accept undefined as a valid input.
Put differently: defaulting based on whether the argument happened to be provided is reporting on what the syntax of the call looked like. But an API doesn't and shouldn't care about the syntax of the call. It just wants a protocol whereby the caller can indicate whether or not they have a value for a given argument. The undefined value is an idiomatic way to do that in JS, and it composes well with other expression forms that already produce undefined when they don't have a value. That puts some burden on programmers not to treat undefined as a normal value, but that's already a cost programmers live with in JS. And that cost is outweighed by the benefit of a more expressive call syntax.
More information about the es-discuss