JavaScript 2.0
Rationale
Named Function Parameters
|
Wednesday, January 29, 2003
Named function parameters had originally been part of the JavaScript 2.0 proposal but were deferred to a future version of the language in order to keep JavaScript 2.0 small. If implemented, named function parameters might behave as described in this section.
The named function parameter extension allows some function parameters to be passed by name instead of by position. Parameter names are simple strings. A function with three positional (one of which is optional) and four named parameters might be declared as:
function f(a: Number, b: Boolean, c: Number = 5, named x: Integer = 7, named y = null, named z = 34, named t = undefined)
Such a function can then be invoked as follows:
f(2, true, y: 5); f(2, true, 8, z: 32, x: 9);
Named parameters are always optional and must include default values. A function call may specify positional arguments followed by named arguments. Positional parameters can only match positional arguments. Named parameters can only match named arguments. The same parameter may not be both positional and named.
The following sections explain the changes to add named function parameters in more detail.
A new non-reserved keyword named named
is added. This keyword is a valid identifier:
A function may take zero or more positional parameters followed by either an optional rest parameter followed by zero or more named parameters or a named rest parameter:
Individual parameters have the forms:
named
NamedParameterCoreIf a Parameter is followed by a =
, then that parameter is optional.
If a function call does not provide an argument for an optional parameter, then that parameter is set to the value of its
AssignmentExpression, implicitly
coerced to the parameter’s type if necessary.
If a parameter is prefixed with named
, then the parameter is matched by name rather than by position. Named
parameters are always optional and must have initializers.
Attempting to define a function with two different parameters with the same name is an error.
A function with named parameters is never unchecked.
If the ...
is present, the function accepts arguments not matched by any of the other listed parameters. If
a parameter is given after the ...
, then that parameter’s identifier is bound to an array of all remaining
arguments. That identifier is declared as a local const
using the parameter’s type, which defaults to Array
.
If the rest parameter is named
, then the parameter’s type is always Array
and cannot be specified
explicitly.
The remaining positional arguments are stored as elements of the rest array with numeric indices starting from 0. If the
rest parameter is not named
, then there must be no remaining named arguments. Otherwise, extra named arguments
are allowed and are stored as named properties of the rest array.
When a function is called, the following list indicates the order of evaluation of the various expressions in a FunctionDefinition. These steps are taken only after all of the argument names and values have been evaluated.
arguments
local variable to an array of
all arguments and their names.undefined
be the first parameter’s
value.The a[
args]
and f(
args)
indexing and function call operators are extended to allow named arguments:
A list of arguments can contain both positional and named arguments. The positional arguments are the subexpressions separated by commas within the ListExpressionallowIn. Named arguments use the same syntax as object literals. All strings except those consisting entirely of digits are legal for argument names. No two arguments may have the same name. Named arguments must follow positional arguments, but otherwise the order of named arguments is immaterial.
Waldemar Horwat Last modified Wednesday, January 29, 2003 |