undefined being treated as a missing optional argument

David Herman dherman at mozilla.com
Fri Apr 13 11:54:18 PDT 2012

On Apr 13, 2012, at 11:46 AM, Allen Wirfs-Brock wrote:

> On Apr 13, 2012, at 11:22 AM, David Herman wrote:
>> On Apr 13, 2012, at 10:53 AM, Allen Wirfs-Brock wrote:
>>> That sentinel could simply be a empty argument position:
>>>        new Intl.Collator( , {usage: "search"});
>> That's not enough. It doesn't allow you to make a dynamic decision as to whether or not to pass that argument, which still leaves you in general with the combinatorial explosion.
> sure you can
> Intl.Collator = function(...args) {
>   let [locale=defaultLocale, options={}] = args;
>   let localeDefaulted = args.hasOwnProperty("0")
>   ...
> }

I'm talking about the caller code. If the caller needs to make a dynamic decision about whether to provide argument 0, it has to write:

    let locale = foo.getLocaleIfTheresOneAvailable();
    locale === undefined ? new Intl.Collator(locale, {usage: "search"})
                         : new Intl.Collator(, {usage: "search"})

That's bad enough with only one. Now when you scale that up to multiple optional arguments you get combinatorial explosion. The semantics that always defaults undefined just allows you to write

    new Intl.Collator(foo.getLocaleIfTheresOneAvailable(), {usage: "search"})


More information about the es-discuss mailing list