当前位置:网站首页>JS 模块化
JS 模块化
2022-07-07 14:11:00 【巴山却话】
目录
1. 模块化的发展史
后端语言很早就有了模块化,如 python 等,但 JS 很长一段时间都没有。
这种结果就是 js 无法将一个大文件,拆分成多个小文件,然后再拼接起来,这就在开发大型的、复杂的项目时非常困难。
但是虽然 JS 官方没有给出模块化,但开发者社区却诞生了几种模块化思想,或者叫规范,如 AMD、CMD、CommonJS等。
在这些规范中,AMD 和 CMD 可以在浏览器端实现模块化,CommonJS 更适合 Node 端,因为其默认无法解决浏览器端的异步加载问题,所以不适合浏览器端使用。
2015 年,在 ES6 发布之际,JS 官方终于给出了自己的模块化方案 ES6 Module(ESM),此种模块化方案可以替代、AMD、CMD和 CommonJS,同时支持 浏览器和服务端的模块化,最终实现大一统的模块化方案。
无论是哪种模块化方案,都支持一些基本的概念
- 一个 js 文件就是一个模块
- 模块中的数据是独立的,其他模块默认无法访问
下面重点谈谈 CommonJS 和 ESM
2. CommonJS
此种模块化规范与 node.js 相伴相生,很长的一段时间内,Node 都只支持这种模块化方案。
使用 exports 或者 module.exports 暴漏数据,使用 require 加载模块
a.js 导出
let name='yhb'
module.exports.name=name
let age=20
module.exports.age=ageb.js 导入
const a_modue=require('./a')
console.log(a_modue)a_module 是一个对象
{ name: 'yhb', age: 20 }a.js 中也可以像下面这样导出数据
let name = 'yhb'
let age = 20
module.exports = {
name,
age
}上面就是 CommonJS 模块化的最基本的内容
3. ES6 模块化
ES6 模块化简称 ESM
ESM 暴漏数据使用 export 或者 export default,加载数据使用 import
而且同时支持服务端和浏览器端
3.1 node 中使用 ESM
node 中如果想更改为使用 ESM,需要在 package.json 中添加 type 属性,并设置值为 module(type 属性的默认值为 commonjs)
type":"module"a.js
export let name = 'yhb'
export function f1() {
console.log('f1')
}b.js
import {name,f1} from './a.js'
f1()a.js 中也可以向下面这样导出
let name='yhb'
let age=20
function sayHello(){
console.log('hello')
}
export {name,age,sayHello}b.js
import {name,age,sayHello} from './a.js'
console.log(name)
console.log(age)
sayHello()b.js 中加载模块时,除了像上面一样,分别指定要加载的模块中的数据外,也可以加载整个模块
import * as a_module from './a.js'
console.log(a_module.name)
console.log(a_module.age)
a_module.sayHello()在上面的代码中,开发者需要知道 a.js 模块中的变量或者函数名称,在加载时或者使用时,名称不能写错,
如下面代码,因为 a.js 中没有 aaa 变量,所以下面的引入会报错
import {name,aaa} from './a.js'是否可以在某种情况下,开发者不需要知道模块中变量或者函数的名字,也可以使用呢?
模块中使用 export default 语法可以很好的解决这个问题
a.js
let name='yhb'
export default nameb.js
import sss from './a.js'
console.log(sss)
export default 相当于导出了一个叫做 default 的变量(default 变量与 name 变量的值相同),b.js 中可以为其随意起一个名字
export default 导出的也可以是一个函数,因为模块外部可以随意为函数起一个名字,所以这里使用匿名函数,函数名称在这里没有意义
export default function (){
console.log('sayHello')
}b.js
import f1 from './a.js'
f1()
因为 expor default 是默认输出,所以每个模块中只能有一个 export default 语句
export default 除了像上面那样,导出一个变量或者函数外,也可以导出一个对象
let name = 'yhb'
let age = 20
export default {
name,
age
}其他模块加载时,不能像没有使用 default 时的写法
import {name,age} from './a.js'而是必须这样写
import a_module from './a.js'
console.log(a_module.name)
console.log(a_module.age)3.2 浏览器中使用 ESM
a.js
let name = 'yhb'
let age = 20
export {
name,
age
}b.js
import {name,age} from './a.js'
function printInfo(){
console.log(`${name}今年${age}岁了`)
}
printInfo()index.html
<script type="module" src="./b.js"></script>如果 b.js 中没有调用 printInfo,而是导出
import {name,age} from './a.js'
function printInfo(){
console.log(`${name}今年${age}岁了`)
}
export {printInfo}index.html 中如果希望调用 printInfo 的话,需要向下面这样写
<script type="module">
import {printInfo} from './b.js'
printInfo()
</script>边栏推荐
- 讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
- Particle effect for ugui
- Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
- 一个普通人除了去工厂上班赚钱,还能干什么工作?
- Numpy -- epidemic data analysis case
- Unity3D_ Class fishing project, bullet rebound effect is achieved
- A link opens the applet code. After compilation, it is easy to understand
- Power of leetcode-231-2
- Notification uses full resolution
- Description of vs common shortcut keys
猜你喜欢

Logback日志框架第三方jar包 免费获取

Power of leetcode-231-2

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

保证接口数据安全的10种方案

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models

Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()

统计学习方法——感知机

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
随机推荐
模仿企业微信会议室选择
MySQL数据库基本操作-DQL-基本查询
尤雨溪,来了!
神经网络c语言中的指针是怎么回事
Leetcode-231-2的幂
Syntaxhighlight highlights the right scroll bar
Iptables only allows the specified IP address to access the specified port
如何在shell中实现 backspace
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
MySQL中, 如何查询某一天, 某一月, 某一年的数据
Performance measure of classification model
TS as a general cache method
47_Opencv中的轮廓查找 cv::findContours()
C4D learning notes 2- animation - timeline and time function
Three. JS introductory learning notes 15: threejs frame animation module
统计学习方法——感知机
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Use moviepy Editor clips videos and intercepts video clips in batches
修改配置文件后tidb无法启动
持续创作,还得靠它!