1. ES6 Module
1.1 export
export
Export module, an object is exported, so when the value changes, the change will follow export content, a plurality of contents can be derived module
export let val = 0
setTimeout(() => {
val = 1
}, 100)
export default
Export specified default output, a module allows only one. The essence is to follow later in value to a default
variable and then export, so the back can only have value with the value of the content or not the statement with statement
1.2 import
import
The introduction and execution module executes at compile time, enhance the effect with a variable
Since the compile-time, you can not use expressions or variables
import { 'f' + 'oo' } from './test' // 报错
The introduction of multiple modules only once
import { foo } from './test'
import { bar} from './test'
// 等同于
import { foo, bar } from './test'
After the run is not allowed to modify the overall load time
import * as test from './test'
test.foo = () => {} // 报错
import
HTML can not run out while require
you can
import test from './test' // 当运行单个js文件时报错
let test = require('./test') // 正常导入模块
1.3 asynchronous loading
The browser loads script
are usually synchronized, the load and execution process will stop rendering. This will result in a bad user experience, and therefore join the asynchronous loading
defer
start downloading the face of the label, execution after the end of page rendering, more defer
can guarantee the order loaded
async
to start downloading the face of the label, file download immediately perform well, more async
can not be guaranteed order
2. CommenJS module
2.1 Modular issues
Due to language problems, Javascript inherently non-modular, so in order to solve this problem there are a lot of programs.
Browser:
<script>
Label reference load, load order problem must be consideredrequire.js
AMD third-party librariessea.js
CMD third-party libraries
eitherCommonJS
,AMD
,CMD
,UMD
,ES6 Modules
are made to addressJavascript
modularity problem
ES6 Modules
is JS modular solutions provided by the official
Node
itself provides a modular and for the 8.5 versionES6 Modules
supports the
2.2 Load Module
- Module is divided into the core module and file module, the core module entirely by the C / C ++ to write called the built-in module
- Priority load modules from the cache, the cache is the object after compiling execution
- The core module is loaded fastest path followed form, custom module slowest (up layer by layer will be
node_modules
in a directory) - When analyzing the package will be the first to
package.json
filemain
attribute specifies the file name entry, then click Find Now if that failsindex.js
,index.json
,index.node
- Within the module and external modules independently of each other, you can not access all the content in addition to export the contents of
2.3 compile modules
- Each module is an object file, by
require.extensions
loading different extension on the method of - By external packaging
function
incomingrequire
,exports
,module
,__filename
,__dirname
and other variables
Import Export Module 2.4
require
Andexports
. may require execution code is loaded modules and get the object to be exported, exports can export the contents of the module you want to exportexports
Parameter is passed, pointing tomodule.exports
empty by default objects.Node
Isexports
, ES6 isexport
- ES6 in
export
can occur more than once and each requires a variable nameexport default
can appear only once Node
It is actually exportedmodule.exports
, butexports = xxx
the syntax is actually modifying the reference parameters, and will not affectmodule.exports
, so the results will lead to failure
2.5 asl
npm init -y // 快速生成package.json文件
npm install --global cnpm // 全局安装cnpm
npm install jquery --regisrty=https://registry.npm.taobao.org // 不安cnpm使用淘宝镜像
npm config set registry https://registry.npm.taobao.org // 将淘宝镜像配置进文件
npm config list // 查看配置文件
2.6 package.json 和package-lock.json
- npm 5 did not have before
package-lock.json
the file, npm only joined after 5 package.json
Basic information file storage project,npm install
when will prevail download dependencies to rely on this document- When the installation package, NPM will generate or update
package-lock.json
the file package-lock.json
The file is savednode_modules
in the information for all packages (version Download), when againnpm install
when you can improve download speedpackage-lock.json
Files can be locked dependent on the version, the new version to prevent automatic upgrades
3. ES6 module and the difference module CommenJS
CommonJS
The input module is a copy of the value, ES6 module output is a reference valueCommenJS
Run-time module is loaded, ES6 module is compiled with the output interface
4. circular reference
Due to practical application, it is difficult to avoid the interdependence between the modules and therefore the module cyclic loading is a problem that must be considered
4.1 CommonJS
模块a
When loading dependence 模块b
, stop loading 模块a
to load 模块b
, 模块b
encountered in the load 模块a
, the return 模块a
part loaded, continue to load 模块b
to the rear, and then returns to load模块a
// test.js
exports.done = false
var test2 = require('./test2')
console.log('在test中,test2.done = %j', test2.done)
exports.done = true
console.log('test执行完毕')
// test2.js
exports.done = false
var test = require('./test')
console.log('在test2中,test.done = %j', test.done)
exports.done = true
console.log('test2执行完毕')
// main.js
var test = require('./test')
var test2 = require('./test2')
console.log('执行完毕')
// 在test2中,test.done = false
// test2执行完毕
// 在test中,test2.done = true
// test执行完毕
// 执行完毕
4.2 ES6 module
Ibid., Still 模块a
loaded 模块b
, 模块b
loaded 模块a
get when 模块a
returning, executing the 模块b
execution after the 模块a
difference is:
ES6
module returns an object, the change will affect the value of the follow-up has returned the
CommonJS
return value is the value of a subsequent change does not affect the return of
Thus the following code, ES6
can be executed, CommonJS
it is not
// even.js
import { odd } from './odd.js'
export let counter = 0
export function even(n) {
counter ++
return n == 0 || odd(n - 1)
}
// odd.js
import { even } from './even.js'
export function odd(n) {
return n != 0 && even(n - 1)
}
// main.js
import * as res from './even.js'
res.even(10)
console.log(res.counter) // 6