Show Module adalah file Javascript yang di dalamnya terdapat value dari objects, functions, dan variables. Kemudian file tersebut dapat diexport dan diimport oleh file lain. Yang mana file lain yang mengimportnya dapat menggunakan values yang ada di file tersebut. Beberapa benefit menggunakan modules adalah sebagai berikut:
Perbedaan export default dengan tidak menggunakan export defaultDalam menuliskan keyword export kadangkala kita menemukan yang diimbuhi dengan keyword default. Ketika kita menggunakan imbuhan default maka ketika import kita tidak perlu menggunakan symbol bracket { }. Namun sebaliknya jika kita tidak menggunakan imbuhan default maka kita harus menggunakan symbol bracket {}. // modules.js Aliases ImportKetika kita mengimport sebuah module kita dapat memberikan alias kepada module tersebut. Tujuannya adalah untuk mempermudah developer memngingatnya. // modules.js Asterisk AliasesMemberikan symbol asterisk pada import adalah menandakan kalau semua function, variables dan object diimport. Namun perlu diperhatikan asterisk aliases harus menambahkan alias. Contohnya adalah sebagai berikut: // modules.js Penggunaan Untuk Node.JSUntuk penggunaan di Node.JS memang ada perbedaan. Letak perbedaanya adalah untuk keyword export dan cara mengimportnya. Mari kita klihat contoh berikut: // modules.js Berikut penjelasan singkat mengenail modules ES6 di Javascript. Semoga bermanfaat. Terima kasih. The In order to use the nameN Identifier to be exported (so that it can be imported via DescriptionEvery module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export. Each type corresponds to one of the above syntax. Named exports:
After the Export declarations are not subject to
temporal dead zone rules. You can declare that the module exports
Default exports:
The As a special case, functions and classes are exported as declarations, not expressions, and these declarations can be anonymous. This means functions will be hoisted.
Named exports are useful when you need to export several values. When importing this module, named exports must be referred to by the exact same name (optionally renaming it with
You can also rename named exports to avoid naming conflicts:
You can rename a name to something that's not a valid identifier by using a string literal. For example:
Re-exporting / AggregatingA module can also "relay" values exported from other modules without the hassle of writing two separate import/export statements. This is often useful when creating a single module concentrating various exports from various modules (usually called a "barrel module"). This can be achieved with the "export from" syntax:
Which is comparable to a combination of import and export, except that
Most of the "import from" syntaxes have "export from" counterparts.
There is also
Attempting to import the duplicate name directly will throw an error.
The following is syntactically invalid despite its import equivalent:
The correct way of doing this is to rename the export:
The "export from" syntax allows the
ExamplesUsing named exportsIn a module
Then in the top-level module included in your HTML page, we could have:
It is important to note the following:
Using the default exportIf we want to export a single value or to have a fallback value for your module, you could use a default export:
Then, in another script, it is straightforward to import the default export:
Using export fromLet's take an example where we have the following hierarchy:
This is what it would look like using code snippets:
Specifications
Browser compatibilityBCD tables only load in the browser See also |