ES6 Module (Export and Export Default)

2022-12-22   ES  

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 interface

The 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

  1. CommonJS output is a copy of a value. ES6 Module via Export {<variable>} output variable reference, the copy of the export default output value
  2. 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
  3. 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

source

Random Posts

Servlet Chapter 8 Knowledge Point Summary -Filter Filter Chain

vSCode configuration .NET Framework project specified version 4.6

Modeling Bargaining and its proof

Python operates the orderly collection of redis

The smallest generation tree || The shortest path is adjacent to the matrix to implement the PRIM algorithm C ++ code