Export Functionality
Commonly exported identifiers in native JavaScript modules include variables, constants, functions, and classes.
Named Exports
The explicit use of identifiers in an export statement is referred to as using named exports in JavaScript. The following example demonstrates the export of multiple functions using named exports.
Example: 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: 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: Function Export using Named Exports 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: 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: 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 };
/