After the ES6 standard is released, module becomes the standard, the standard use is to export the interface with export instruction, import into the module, but in our usual node module, we use the COMMONJS specification, using require to introduce the module, Export the interface using Module.exports. Node's module follows the COMMONJS specification, Requirejs follows the Amd,seajs follow the CMD, although each has a different, but in short still want to maintain a more uniform code style. ES6 released module does not directly adopt COMMONJS, even require is not adopted, that is, require is still just a private global method of node, Module.exports is also just a global variable attribute of node's private, which has nothing to do with the standard half-dime relationship. Require is run-time, import it is compile-time, it must be placed at the beginning of the file, and the use of the format is also determined. In JavaScript ES6, the export default and export differences: 1.export and export default can be used to export constants, functions, files, modules, etc. 2. You can pass import+ (constant | function |) in other files or modules. Pieces | module) name, and import it so that it can be used 3. In a file or module, export, import can have multiple, export default has only one 4. Export by exporting, add {},export The default does not require 1,
// Demo1.js Const ' Hello World ' export function f (a) { return A +1}
The corresponding import method:
// Demo2.js from ' Demo1 ' // can also be written separately two times, when imported with curly braces
Import needs to know the identity of the variable exposed by the Demo.js, otherwise it cannot be loaded
2.
// Demo1.js default Const ' Hello World '
The corresponding import method:
// Demo2.js from ' Demo1 ' // no curly braces at the time of import
Using the Export default command, specify the default output for the module so that you do not need to know the variable name of the module to be loaded
JS Module specification