undefined being treated as a missing optional argument

Brendan Eich brendan at mozilla.org
Fri Apr 13 10:10:01 PDT 2012


Allen Wirfs-Brock wrote:
> On Apr 12, 2012, at 11:25 PM, Norbert Lindenberg wrote:
>
>> >  What about functions that take two or more independent optional arguments, where sometimes you want to omit the first argument while providing the second?
>
> arguably, this is where you should be using an options argument rather than multiple positional parameters with default values.  Certainly for the "or more" case.

Ok, so use an options argument and then add delegation:

function add({x = 0, y = 0}) {
   return x + y;
}
function inc({opt_y}) {
   return add({x:1, y:opt_y});
}
assertEq(1, inc({}));

The author surely wants y to default to 0 and for the assertEq to succeed.

In general, delegation (depth D) plus optionality (degree N paramters) 
makes an (2N)^D worst-case combinatorial explosion.

This is IMHO a strong argument for a sentinel in-language to mean 
"missing actual".

/be


More information about the es-discuss mailing list