当前位置:网站首页>ES6 module
ES6 module
2022-06-26 13:19:00 【HaanLen】
summary
- stay ES6 front , Modularity is achieved using
RequireJSperhapsseaJS( The difference is based onAMDStandardized modular Library , And based onCMDStandardized modular Library ). - ES6 The introduction of modularity , Its design idea is to be able to determine the dependency of modules at compile time , And the input and output variables .
- ES6 The modularization of is divided into export (
export) @ And import (import) Two modules .
characteristic
- ES6 The module automatically turns on strict mode , Whether or not you add
use strict;. - Various types of variables can be imported and exported in the module , Such as function , object , character string , Numbers , Boolean value , Class etc. .
- Each module has its own context , The variables declared in each module are local variables , Does not contaminate the global scope .
- Each module is loaded only once ( Is a singleton ), If you load the same file in the same directory , Read directly from memory .
export And import
Basic usage
- The exported function declaration and class declaration must have a name (
export defaultOrder another consideration ). - You can export not only declarations, but also references ( For example, functions ).
exportCommands can appear anywhere in the module , But it must be at the top of the module .importThe command will be raised to the head of the whole module , First, execute .
modularization : How to define modularity ; How to use modules
Be careful : It needs to be put into the server environment .
Take a simple chestnut
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import './module/tt.js' </script>
</body>
</html>
js
console.log('1 Module loaded ');
export const a=12;

import Can be a relative path 、 It could be an absolute path .
import The module will only import once , No matter how many times you import .
import Export to module content , If there is a timer change , There will be changes outside .
Chestnut display
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import {
a,b,c} from './module/tt.js'; console.log(a,b,c); </script>
</body>
</html>
js
console.log('1 Module loaded ');
export const a=12;
export const b=11;
export const c=3;

Chestnut display
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import {
a,b,c} from './module/tt1.js'; console.log(a,b,c); </script>
</body>
</html>
js
const a=12;
const b=11;
const c=3;
export{
a,
b,
c
}

Alias - Chestnut display
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import {
aaa,bbb,ccc} from './module/tt1.js'; console.log(aaa,bbb,ccc); </script>
</body>
</html>
js
const a=12;
const b=11;
const c=3;
export{
a as aaa,
b as bbb,
c as ccc
}

Chestnut display
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import {
aaa as a,bbb as b,ccc as c} from './module/tt1.js'; console.log(a,b,c); </script>
</body>
</html>
js
const a=121;
const b=11;
const c=34;
export{
a as aaa,
b as bbb,
c as ccc
}

Chestnut display
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import * as mod from './module/tt1.js'; console.log(mod); </script>
</body>
</html>
js
const a=121;
const b=11;
const c=34;
export{
a as aaa,
b as bbb,
c as ccc
}

console.log(mod.aaa);//121
Chestnut display -export defaulr xxx
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import a from './module/tt2.js'; console.log(a);//12 </script>
</body>
</html>
js
export default 12;
Example
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"> </style>
</head>
<body>
<script type="module"> import mod,{
show,sum,a,b} from './module/tt2.js'; let p1=new mod.Person('ddd',18); console.log(p1.showName()); console.log(p1.showAge()); show(); sum(); console.log(a,b); </script>
</body>
</html>
js
const a=12;
const b=21;
const sum=()=>{
console.log(a+b);
return a+b;
}
const show=()=>{
console.log(" Yes show");
return 1;
}
class Person{
constructor(name,age){
this.name=name;
this.age=age;
}
showName(){
return ` The name is :${
this.name}`;
}
showAge(){
return ` Age is : ${
this.age}`;
}
}
export{
a,
b,
show,
sum
}
export default {
Person};

Example
ff.js
export const a=12;
export const b=21;
import {
a,b} from './ff.js'
const sum=()=>{
console.log(a+b);
return a+b;
}
const show=()=>{
console.log(" Yes show");
return 1;
}
class Person{
constructor(name,age){
this.name=name;
this.age=age;
}
showName(){
return ` The name is :${
this.name}`;
}
showAge(){
return ` Age is : ${
this.age}`;
}
}
export{
a,
b,
show,
sum
}
export default {
Person};
Examples show
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
</style>
</head>
<body>
<script type="module">
import {
a,b} from './module/ff.js';
console.log(a);
setTimeout(()=>{
console.log(a);
},3000);
</script>
</body>
</html>
js
export let a=12;
export let b=5;
setTimeout(()=>{
a=123;
},2000);

import You can also dynamically load modules
Example
<script type="module">
import('./module/ff.js').then(res=>{
console.log(res.a*res.b);//60
});
</script>
export let a=12;
export let b=5;
Promise.all()
<script type="module">
Promise.all({
import('./module/1.js');
import('./module/2.js');
}).then(([mod1,mod2])=>{
console.log(mod1);
console.log(mod2);
})
</script>
async
async function fn(){
const mod1=await import('./module/1.js');
const mod1=await import('./module/2.js');
console.log(mod1,mod2);
const [m1,m2]=await Promise.all([
import('./module/tt.js');
import('./module/ff.js');
]);
console.log(m1,m2);
}
fn();
边栏推荐
- HDU1724[辛普森公式求积分]Ellipse
- QT . Establishment and use of pri
- scrapy——爬取漫画自定义存储路径下载到本地
- Electron official docs series: Best Practices
- Map value
- LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
- Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller
- Processing polyhedron change
- E - Apple Catching
- Electron official docs series: Contributing
猜你喜欢

Beifu realizes the control of time slice size and quantity through CTU and ton

Arcpy - - utilisation de la fonction insertlayer (): ajout de calques dans un document de carte

Electron official docs series: Get Started

Fire warning is completed within 10 seconds, and Baidu AI Cloud helps Kunming Guandu build a new benchmark of smart city

装饰器(Decorator)

What features are added to Photoshop 2022 23.4.1? Do you know anything

Script - crawl the customized storage path of the cartoon and download it to the local
![P5733 [deep foundation 6. example 1] automatic correction](/img/34/081dbd805593a92a86c3081d6772e3.png)
P5733 [deep foundation 6. example 1] automatic correction

Processing function translate (mousex, mousey) learning

Detailed explanation of C const: definition and use of C constant
随机推荐
Beifu cx5130 card replacement and transfer of existing authorization files
C语言:练习题二
Opencv high speed download
装饰器(Decorator)
Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
Processing random generation line animation
Typescript
倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试
Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
Hdu1724[Simpson formula for integral]ellipse
中国剩余定理模板题 互质与非互质
code force Party Lemonade
Word document export (using fixed template)
Electron official docs series: Examples
Prototype
Electron official docs series: Distribution
Script - crawl the customized storage path of the cartoon and download it to the local
Vivado error code [drc pdcn-2721] resolution
Digital signal processing -- Design of linear phase type (Ⅰ, Ⅲ) FIR filter (1)
Electron official docs series: Testing And Debugging