ES6 Module uses Import keyword import module, Export keyword export module
ES6 Module is static, and has the same improvement effect as VAR and Function
ES6 Module automatically adopts a strict mode (the top layer This return UNDEFINED)
ES6 Module supports the use of Export {<variable>} exported interface, or Export Default exports anonymous interfaceThe difference between
Export and export default:
Export {<variable>} Export is a reference to a variable, which changes with the value in the original file; Export Default exports a value, which will not change, equivalent to constant constant
eg:
a.js
let x = 10;
let y = 20;
setTimeout(() => {
x = 100;
y = 200;
},1000)
export {x}
export default y
b.js
import {x} from './a.js'
import y from './a.js'
console.log(x,y) //10,20
setTimeOut(() => {
console.log(x,y) //100,20
},1000)
PS: The difference between ES6 MODULE and Commonjs
- CommonJS output is a copy of a value. ES6 Module via Export {<variable>} output variable reference, the copy of the export default output value
- Commonjs runs on the server and is designed to load when it is designed, that is, the code is executed to load the module, and ES6 is pre -loaded
- Commonjs has a cache, and then the result of the first run is loaded, and ES6 Module does not.
Return Import to implement dynamic loading modules
if(true){
import('./a.js').then(res => {
console.log(res)
})
}
————————————————
Copyright Statement: This article is an original article of the CSDN blogger “Judyc”. Following the CC 4.0 BY-SA copyright agreement, please attach the original link and this statement.
Original link: https://blog.csdn.net/judyc/article/details/87794180