undefined being treated as a missing optional argument

Oliver Hunt oliver at apple.com
Fri Apr 13 11:35:12 PDT 2012


What happens if i have:

function foo(a=1, b=2) { log(a, b, arguments.length); }

foo();
foo(undefined);
foo(3);
foo(undefined, undefined);
foo(undefined, 3);
foo(3, undefined);

Default values are for when arguments are not passed, it does not make logical sense to say that they're the value given just because someone has passed undefined.  It also makes the behavioural definition more complex.

--Oliver

On Apr 13, 2012, at 11:26 AM, David Herman <dherman at mozilla.com> wrote:

> On Apr 13, 2012, at 9:38 AM, Russell Leggett wrote:
> 
>> Yes, but as I said, and Erik pointed out is in the wiki, it is a lot more likely that someone would pass f(foo) or f(obj.foo) where foo might be undefined.
> 
> Bingo.
> 
>> Expecting undefined as a possible valid argument (as opposed to a missing argument) seems like a very rare case, and probably a code smell.
> 
> Amen.
> 
>> I think it would be very unintuitive to the majority of JavaScript developers, and greatly undermines the usefulness of default parameters for the sake of a minority use case.
> 
> Preach it.
> 
>> In those cases where undefined is an acceptable parameter, just don't use default parameters.
> 
> +9001, as rwaldron likes to say (big fan of ISO?)
> 
> Dave
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list