var foo = arg || defaultFoo idiom for a function taking an optional parameter arg motivates one or both of||=, or perhaps ??= (a more precise operator inspired by C#)||=) FormalParameterList ::=
Identifier
| FormalParameterList , Identifier
FormalParameterList ::=
FormalParameter
| FormalParameterList , FormalParameter
FormalParameter ::=
Identifier Initialiser?
| Pattern Initialiser?
= AssignmentExpression= AssignmentExpression was given to supply P with a default value, then:— Brendan Eich 2009/05/05 06:06
The length property of a function should be updated to not include optional parameters.
— Erik Arvidsson 2010/11/05 22:37
Right now this works as if arguments.length is checked. However, sometimes when chaining functions with optional parameters this will lead to problems since the optional parameter will be passed as undefined. Would it make sense to also allow undefined in the tail of the parameter list?
This example is pretty contrived and it can easily be fixed but I think it points out the issue clearly:
function add(x = 0, y = 0) { return x + y; } function inc(opt_x) { return add(1, opt_x); } assert(1, inc());
— Erik Arvidsson 2011/05/03 16:52
We could treat trailing undefined as missing. CoffeeScript (after apply and call in their first parameter handling, and after ==) equate null and undefined in this way. You can’t therefore pass an explicit null or undefined, and I expect that will be a sticking point.
The scenario you describe above could be handled by passing a “magic undefined” to inc when it is called via inc(), and propagating that through to add to trigger defaulting.
This is doable but a hassle for implementors (not terrible from where I sit, but other implementors should weigh in; JavaScriptCore already lets objects and/or strings masquerade as undefined, IIRC).
— Brendan Eich 2011/05/03 18:34