Export Functionality

Commonly exported identifiers in native JavaScript modules include variables, constants, functions, and classes.

Topics

Named Exports

The explicit use of identifiers in an export statement is referred to as using named exports in JavaScript.

Example 5-2 demonstrates the export of multiple functions using named exports.

Example 5-2 Function Export using Named Exports

This code snippet creates a module called named_exports_module, defines two functions, sum() and difference(), and then uses a named export to provide access for other modules to import the listed functions.

CREATE OR REPLACE MLE MODULE named_exports_module LANGUAGE JAVASCRIPT AS

function sum(a, b) {
    return a + b;
}

function difference(a, b) {
    return a - b;
}

export {sum, difference};
/

Make note of the export{} statement at the end of the module. Named exports require the use of curly brackets when listing identifiers. Any identifier placed between the curly brackets is exported. Those not listed are not exported.

Rather than using the export statement towards the end of the module, it is also possible to prefix an identifier with the export keyword inline. The following example shows how the same module from the previous example can be rewritten with the export keyword provided inline with the JavaScript code.

Example 5-3 Function Export Using Export Keyword Inline

This code snippet creates a module called inline_export_module and defines two functions, sum() and difference(), which are both prefaced with the export keyword inline.

CREATE OR REPLACE MLE MODULE inline_export_module LANGUAGE JAVASCRIPT AS

export function sum(a, b) {
    return a + b;
}

export function difference(a, b) {
    return a - b;
}
/

Both named_exports_module from Example 5-2 and inline_export_module are semantically identical. The method used to export the functions is the only syntactical difference.

Default Exports

As an alternative to named exports, a default export can be defined in JavaScript. A default export differs syntactically from a named export. Contrary to the latter, a default export does not require a set of curly brackets.

Note:

In line with the ECMAScript standard, only one default export is possible per module.

Example 5-4 Export a Class Using a Default Export

The following code snippet creates a module called default_export_module, defines a class called myMath, and defaults the class using a default export.

CREATE OR REPLACE MLE MODULE default_export_module 
LANGUAGE JAVASCRIPT AS

export default class myMath {

    static sum(operand1, operand2) {
        return operand1 + operand2;
    }

    static difference(operand1, operand2) {
        return operand1 - operand2;
    }
}
/

Private Identifiers

Any identifier not exported from a module is considered private and cannot be referenced outside the module's scope or in module call specifications.

Example 5-5 Named Export of Single Function

The following code snippet creates a module called private_export_module, defines two functions, sum() and difference(), and exports the function sum() via named export. The function difference() is not included in the export statement, thus is only available within its own module's scope.

CREATE OR REPLACE MLE MODULE private_export_module
LANGUAGE JAVASCRIPT AS

function sum(a, b) {
    return a + b;
}

function difference(a, b) {
    return a - b;
}

export { sum };
/