Overview
Before ES6, RequireJS or seaJS was used to implement modularization (these are modularization libraries based on the AMD specification and the CMD specification, respectively).
ES6 introduced modularization, with the design philosophy of determining module dependencies and input/output variables at compile time.
ES6 modularization is divided into two modules: export (@) and import.
Features
ES6 modules automatically enable strict mode, whether or not you add it at the top of the module.use strict;。
Modules can import and export various types of variables, such as functions, objects, strings, numbers, booleans, classes, etc.
Each module has its own context. Variables declared in each module are local variables and do not pollute the global scope.
Each module is loaded only once (it is a singleton). If the same file in the same directory is loaded again, it is read directly from memory.
export and import
Basic Usage
Modules import and export various types of variables, such as strings, numbers, functions, and classes.
- Exported function declarations and class declarations must have names (the export default command is a separate consideration).
- Not only can declarations be exported, but references (such as functions) can also be exported.
- The export command can appear anywhere in the module, but it must be at the top level of the module.
- The import command is hoisted to the top of the entire module and executed first.
It is recommended to use curly braces to specify a set of variables to be exported, written at the end of the document, to clearly define the exported interfaces.
Functions and classes both need corresponding names. Exporting at the end of the document also avoids the lack of corresponding names.
Usage of as
The interface names exported by the export command must have a one-to-one correspondence with the variables inside the module.
The imported variable names must match the exported interface names; the order can be different.
When interface names exported by different modules have duplicate naming, use as to redefine the variable names.
Features of the import command
Read-only property: In the script that loads the module, it is not allowed to rewrite the reference of the interface. That is, you can modify the property values of imported variables that are of object type, but you cannot modify the values of imported variables that are of primitive type.
Singleton pattern: Executing the same import statement multiple times will only execute once, not multiple times. Importing the same module and declaring different interface references will declare the corresponding variables, but the import will only execute once.
Static execution feature: import is statically executed, so expressions and variables cannot be used.
The export default command
- In a file or module, there can be multiple export and import statements, but only one export default.
- In export default, default is the corresponding exported interface variable.
- When exporting using export, you must add { } when importing; export default does not require it.
- The members exposed by export default can be received using any variable.
Combined Usage
Note: import() is a proposal; we will not elaborate on it here for now.
export and import can be used in the same module, with the following features:
- You can rename the exported interfaces, including default.
- Combining export and import can also export everything; the interfaces exported by the current module will override the inherited exports.