Mascara: Next Generation JavaScript compiler

Try it Download Buy About Documentation Blog Contact

Functions

Functions and methods are defined with the same syntax. Methods are just functions defined in a class-body.

Syntax:

Syntax definition [Help]
function functionname ( [ parameters ] ) 
                        [ ( : | like ) returntype ] 
{
    statements
}

See parameters for parameter syntax.

A function can optionally have type annotations which indicates the expected parameter types and return values:

function sum(a : Number, b : Number) : Number {
   return a + b;
}
While type annotations are always optional, it is especially recommended to add type annotations to function parameters.

Shorthand function syntax

If the function contains only a single expression, a simpler syntax without the curly brackets and explicit return can be used:

Syntax definition [Help]
function functionname ( [ parameters ] ) 
                        [ ( : | like ) returntype ] 
    expression
Example:
function add(a,b) a+b;
is equivalent to
function add(a,b) { 
    return a+b; 
}

See also: Getters and setters for functions that disguise as properties.

X Try!
Mascara JavaScript Generated JavaScript

Translating...
Show compiler settings