Mascara: Next Generation JavaScript compiler

Try it Download Buy About Documentation Blog Contact

Parameters

Parameters are used in the defintion of functions and constructors.

Parameters are separated by comma. There are positional, optional and rest parameters.

Positional parameters

Syntax definition [Help]
parametername [ ( : | like ) type ]    

Example:

function x(a, b : string) {}
The function has two parameters. The second parameter has a type annotation, which indicates that the function must be called with a value of the specified type. (Se like for explanation of the like-constraint).

Optional parameters

Syntax definition [Help]
parametername [ ( : | like ) type ]  = expression  
An optional parameter is provided with a default value. If a value is not provided in the method call, the parameter gets the default value.

function x(c = 100) { return c; }
x(17); // returns 17
x(); // returns 100

Rest parameter

Syntax definition [Help]
... parametername 
The rest parameter becomes an array of all arguments in excess of the ones that matches other parameters. Example:
function x(...d) {
    return d.length;
}
x(1,2,3); // returns 3
x(); // returns 0

A rest parameter must always be the last parameter in the parameter list.

X Try!
Mascara JavaScript Generated JavaScript

Translating...
Show compiler settings