Objective
Many years ago, we introduced code that was introduced directly with script SRC, or wrote a loadscript on its own.
With script Src Way, when the project JS file more, there will be a lot of script tags on the page, put aside performance for the time being, the code is very ugly, and very poor maintenance, especially when the JS file has a dependency, more difficult to deal with.
In order to solve this pain point, to handle the load and dependency of JS, and to promote JS modular development, so there is a browser-side CMD, AMD and other specifications for the service side (such as Nodejs) COMMONJS specifications, as well as the final standard ES6 module specification. Get a rough look at these things first.
Cmd:common module Definition-Public module definitions, is Mr. Yuber's seajs in the promotion process of the module definition of the normalized output.
Amd:asynchromous Module Definition-asynchronous modules is defined as the normalized output that Requirejs defines for a module during the promotion process.
COMMONJS Specification: Service-side specification, file-as-a-module. Export the module with Module.exports, require introduce the file/module.
ES6 MODULE:ES6 module Standard.
Grammar and use
CMD and AMD do not make too much of a description. Want to know about the children's shoes please search in the morning. Here the main records ES6 Module and CommonJs
ES6 Module
The ES6 Module automatically adopts strict mode, regardless of whether you have a wood and add ' use strict ' to the code.
Export exported variables, import imported variables. You should be familiar with children's shoes after the end of the language.
// export can derive variables, objects, functions, and classes.
= ' Linux '= ' captain '= ' pwd '; export { username, password = (x) + x + x; Export Class fun{}
Note that the Export command specifies an external interface, so ensure that the interface name must establish a one by one corresponding relationship with the internal variables of the module. The meaning of this sentence is that you cannot define a good variable, just export the variable directly.
Either export at the time of definition, or export an object at the end of the file, which is the variable to be exported and a key-value pair relationship.
// error export 1; // correct Const T = 1; export { T}// error Const TEST = () = {}export test; // correct export TEST = () = {}export { Test }
Es6-module and Commonjs