Background
AKA “optional parameters” and “optional arguments”, but without arity checking, all trailing arguments are optional in a real sense
-
-
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#)
default parameter values (these may abate the need for ||=)
Goals
Allow trailing formal parameters to receive default values if no corresponding actual parameters are passed
The default values should be computed in the scope of the callee, thus enabling use of
any parameters to the left of the one being given its default value, and of
any lexical and free (global) variables in enclosing functions and the top level
Syntax
FormalParameterList ::=
Identifier
| FormalParameterList , Identifier
FormalParameterList ::=
FormalParameter
| FormalParameterList , FormalParameter
FormalParameter ::=
Identifier
| Identifier = AssignmentExpression
Semantics
Issues
It’s easy enough to elaborate the grammar to require that only trailing formal parameters may be given default values. Worth doing?
Clearly ES5’s 10.6 steps under 3(d) could be adapted, but I’m both (a) lazy and (b) hopeful that we’ll switch from pseudocode to a testable spec
— Brendan Eich 2009/05/05 06:06