How to export a Class in JavaScript

 

An export declaration is used to export values ​​from a JavaScript module. The exported values ​​can then be imported into other programs using an import declaration or dynamic import. The value of an imported binding can change in the module that exports it - when a module updates the value of a binding it exports, the update will be visible in its imported value.

In order to use an export declaration in a source file, the file must be interpreted by the runtime as a module. In HTML, this is done by adding type="module" to the <script> tag or by importing another module. Modules are automatically interpreted in strict mode.

 

Each module can have two different export types, a named export and a default export. You can have multiple named exports per module, but only one default export. Each type corresponds to one of the above syntaxes.

 


export { myFunction2, myVariable2 };

// export individual features (can export var, let,
// const, function, class)
export let myVariable = Math.sqrt(2);
export function myFunction() { /* ... */ };

 

After the export keyword, you can use let, const, and var declarations, as well as function or class declarations. You can also use the syntax export { name1, name2 } to export a list of names declared elsewhere. Note that export {} does not export an empty object - it is a no-operation declaration that exports nothing (an empty list of names).

Export declarations are not subject to the temporary dead zone rules. You can declare that a module exports X before the X name itself is declared.

export { x };
const x = 1;

Tags:

Share:

Related posts