Js modular import and export
CommonJs
, AMD
, CMD
, ES6
Are defined in specifications for modular use, for which the dependency between the processing module and the introduction of standardized modules and resolve naming conflicts, and to make use of a modular approach to decompose complex systems code structure is more reasonable , A manageable module with higher maintainability.
CommonJS
CommonJS
It is NodeJs
the specification of server-side modules. According to this specification, each file is a module with its own scope. Variables, functions, and classes defined in a file are private and invisible to other files. CommonJS
The specification stipulates that within each module, module
variables represent the current module. This variable is an object, and its exports
attributes are external interfaces. Loading a module is actually loading the module exports
properties. In short, the CommonJS
specifications are require
imported module.exports
and exports
exported.
// 1.js
var a = 1;
var b = function(){
console.log(a);
}
module.exports = {
a: a,
b: b
}
/*
// 当导出的模块名与被导出的成员或方法重名时可以有如下写法
module.exports = {
a,
b
}
*/
// 2.js
var m1 = require("./1.js")
console.log(m1.a); // 1
m1.b(); // 1
It can also be used exports
for export, but you must not rewrite exports
the pointer, because it exports
is only a pointer and points to module.exports
the memory area, that is exports = module.exports = {}
, rewriting exports
changes the pointer to the point that the module cannot be exported, which simply exports
provides a simple solution for writing , In the end, in fact, all use module.exports
export. Also if used simultaneously in one file module.exports
and exports
then export only module.exports
content
// 1.js
var a = 1;
var b = function(){
console.log(a);
}
exports.a = a;
exports.b = b;
// exports = { a, b } // 不能这么写,这样就改变了exports的指向为一个新对象而不是module.exports
// 2.js
var m1 = require("./1.js")
console.log(m1.a); // 1
m1.b(); // 1
AMD
AMD
The specification is not AMD YES
, the AMD
definition of asynchronous module, the full name of the Asynchronous Module Definition
specification, is a modular solution on the browser side. The CommonJS
specification introduction module is loaded synchronously, which is not a problem for the server side, because its modules are stored on the hard disk and can wait for synchronous loading to complete, but In the browser, the module is loaded through the network. If it is synchronously blocked and waits for the module to load, there may be a situation where the browser page is suspended AMD
. The module is loaded asynchronously. The loading of the module does not affect the operation of the statements behind it. All statements that depend on this module are defined in a callback function. This callback function will not run until the loading is completed, which RequireJS
is the AMD
specification.
require(['moduleA', 'moduleB', 'moduleC'], function (moduleA, moduleB, moduleC){
// do something
});
define(['moduleA', 'moduleB', 'moduleC'], function (moduleA, moduleB, moduleC){
// do something
return {};
});
/**
define和require在依赖处理和回调执行上都是一样的,不一样的地方是define的回调函数需要有return语句返回模块对象(注意是对象),这样define定义的模块才能被其他模块引用;require的回调函数不需要return语句,无法被别的模块引用
*/
// html的<script>标签也支持异步加载
// <script src="require.js" defer async="true" ></script> <!-- async属性表明这个文件需要异步加载,避免网页失去响应。IE不支持这个属性,只支持defer,所以把defer也写上。 -->
CMD
CMD
The general module definition is SeaJS
the standardized output of the module definition in the promotion process, and is also a modular asynchronous solution on the browser side. The difference between the CMD
and AMD
is mainly due to:
- For dependent modules, it
AMD
is executed in advance (relatively defined callback function, theAMD
loader is to load all dependencies in advance and call the execution and then execute the callback function), itCMD
is delayed execution (relatively defined callback function, theCMD
loader is to load all dependencies After executing the callback function, when the execution requires the dependent module, the loaded dependency is called and returned to the callback function), butRequireJS
from the2.0
beginning, it can also be changed to delay execution AMD
It is dependent on the front (when you define the module, you must declare the module it depends on), and itCMD
is the closest to the dependency (only go when yourequire
need a certain module -load on demand, return immediately after use ).
define(function(require,exports,module){
var a = reuire('require.js');
a.dosomething();
return {};
});
ES6
ES6
The function of the module is implemented at the level of language standards, in order to become a common module solution for browsers and servers. The ES6
standard uses export
and export default
to export the module, and uses the import
import module. In addition , the module that can be used require
to import export
and export default
export in the browser environment is still recommended, but it is still recommended to use the import
standard import module.
export
The export default
main differences are as follows:
export
Can be imported on demand,export default
noexport
There can be multiple,export default
only oneexport
Can directly export variable expressions,export default
noexport
Export by way of{}
,export default
it needs to be added during import , it is not necessary
// 1.js
var a = 1;
var b = function(){
console.log(a);
}
var c = 3;
var d = a + c;
var obj = { a,b,c }
export {a,b};
export {c,d};
export default obj;
<!-- 3.html 由于浏览器限制,需要启动一个server服务 -->
<!DOCTYPE html>
<html>
<head>
<title>ES6</title>
</head>
<body>
</body>
<script type="module">
import {a,b} from "./1.js"; // 导入export
import m1 from "./1.js"; // 不加{}即导入export default
import {c} from "./1.js"; // 导入export 按需导入
console.log(a); // 1
console.log(b); // ƒ (){ console.log(a); }
console.log(m1); // {a: 1, c: 3, b: ƒ}
console.log(c); // 3
</script>
</html>
reference
https://segmentfault.com/a/1190000010426778
https://www.cnblogs.com/leftJS/p/11073481.html
https://www.cnblogs.com/zhoulujun/p/9415407.html
https://www.cnblogs.com/zhoulujun/p/9415407.html
https://www.cnblogs.com/moxiaowohuwei/p/8692359.html