undefined being treated as a missing optional argument

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Apr 13 10:27:10 PDT 2012

On Apr 13, 2012, at 9:50 AM, Russell Leggett wrote:

> 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.
> Ok, sure, but in an equally likely case, only one of the arguments is a pass through, should you still use ...args for one thing just to distinguish? What about other case like objects with optional properties fadeIn(config.duration)?

One of the nice things about ES6 is that we have destructuring  and default/rest arguments.  The defaulting semantics of destructuring binding is currently  exactly the same (they use the same semantic rules)  as formal parameter binding.  that means you can almost[*] always replace:
   function f(<formals>) { ...


   function f(...args) {
      let [<formals>] = args; 

If you want to treat some argument one way in certain circumstances and another way in other circumstances all you have to do is do one or more manual destructurings such as above. 

[*] It's "almost" because slightly different shadowing rules are used when processing default value expression in the formals parameters position. Such differences can be avoid if you are careful in your name selection.

> I just don't understand why someone who wanted to be able to distinguish between missing and undefined would really need the extra convenience of default parameters. What are they going to default to - undefined? Meanwhile, the rest of the people that probably could use it all the time to replace their foo = foo || "default" code have to keep it around. I understand what you're saying in principle. There is a certain amount of purity and correctness to it, I just don't think its practical.

      function f(foo="default") {}

if not semantically the same as

     function f(foo) {foo == foo || "default"}


f(null), f(false), f(0), f(NaN), f("")


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120413/98a1a8d5/attachment.html>

More information about the es-discuss mailing list