当前位置:网站首页>ES6模块
ES6模块
2022-06-26 12:36:00 【HaanLen】
概述
- 在 ES6 前, 实现模块化使用的是
RequireJS或者seaJS(分别是基于AMD规范的模块化库, 和基于CMD规范的模块化库)。 - ES6 引入了模块化,其设计思想是在编译时就能确定模块的依赖关系,以及输入和输出的变量。
- ES6 的模块化分为导出(
export) @与导入(import)两个模块。
特点
- ES6 的模块自动开启严格模式,不管你有没有在模块头部加上
use strict;。 - 模块中可以导入和导出各种类型的变量,如函数,对象,字符串,数字,布尔值,类等。
- 每个模块都有自己的上下文,每一个模块内声明的变量都是局部变量,不会污染全局作用域。
- 每一个模块只加载一次(是单例的), 若再去加载同目录下同文件,直接从内存中读取。
export 与 import
基本用法
- 导出的函数声明与类声明必须要有名称(
export default命令另外考虑)。 - 不仅能导出声明还能导出引用(例如函数)。
export命令可以出现在模块的任何位置,但必需处于模块顶层。import命令会提升到整个模块的头部,首先执行。
模块化:如何定义模块化;如何使用模块
注意:需要放到服务器环境。
举个简单栗子
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模块加载了');
export const a=12;

import可以是相对路径、也可以是绝对路径。
import模块只会导入一次,无论导入多少次。
import导出去模块内容,如果里面有定时器更改,外面也会改动。
栗子展示
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模块加载了');
export const a=12;
export const b=11;
export const c=3;

栗子展示
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
}

别名-栗子展示
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
}

栗子展示
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
}

栗子展示
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
栗子展示-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;
例子
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("执行了show");
return 1;
}
class Person{
constructor(name,age){
this.name=name;
this.age=age;
}
showName(){
return `名字是:${
this.name}`;
}
showAge(){
return `年龄是: ${
this.age}`;
}
}
export{
a,
b,
show,
sum
}
export default {
Person};

例子
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("执行了show");
return 1;
}
class Person{
constructor(name,age){
this.name=name;
this.age=age;
}
showName(){
return `名字是:${
this.name}`;
}
showAge(){
return `年龄是: ${
this.age}`;
}
}
export{
a,
b,
show,
sum
}
export default {
Person};
例子展示
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也可以动态加载模块
例子
<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();
边栏推荐
- PHP returns false when calling redis method decrby less than 0
- Assembly language (7) operation instruction
- 2022 edition of China's energy and chemical industry market in-depth investigation and investment feasibility analysis report
- SQL injection in Pikachu shooting range
- 源码学习:AtomicInteger类代码内部逻辑
- Typescript learning (I) type
- 无人机遥感在森林监测的部分应用研究案例总结
- Example of parameter passing from laravel query constructor to closure method
- Mysql8 master-slave replication
- guacamole安装
猜你喜欢

Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology

快手实时数仓保障体系研发实践

不到40行代码手撸一个BlocProvider

Examples of how laravel uses with preload (eager to load) and nested query
![[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure](/img/d2/a6cbb0abe9e04c412d1f6021430528.png)
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
The loss of female scientists

power designer - 自定义注释按钮

小白懒人专用-win10-win11一键安装版

文件远程同步、备份神器rsync

机组实践实验8——使用CMStudio设计基于基本模型机微程序指令(1)
随机推荐
2022 edition of China's medical robot industry investment status investigation and prospect dynamic analysis report
一个初级多线程服务器模型
Less than 40 lines of code to create a blocprovider
Laravel subdomain accesses different routing files and different modules
PHP returns false when calling redis method decrby less than 0
Current situation investigation and investment prospect forecast analysis report of China's electrolytic copper market from 2022 to 2028
小程序中控件里面的内容较多,让其支持滚动的良好方案
2022 edition of Beijing 5g industry investment planning and development prospect forecast analysis report
KITTI Detection dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h
JMeter response time and TPS listener tutorial
Several rare but useful JS techniques
710. 黑名单中的随机数
Realize microservice load balancing (ribbon)
2022 edition of China's cotton chemical fiber printing and dyeing Market Status Investigation and Prospect Forecast Analysis Report
美学心得(第二百三十八集) 罗国正
China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
TP5 thinkphp5 report serialization of'closure'is not allowed
Microservice governance (nocas)
NLP-D60-nlp比赛D29
Research on the current situation of China's modified engineering plastics market and demand forecast analysis report 2022-2028