当前位置:网站首页>JS modularization
JS modularization
2022-07-07 16:20:00 【Bashan queer dialect】
Catalog
1. The history of modularity
Back end languages have long been modular , Such as python etc. , but JS Not for a long time .
The result is js Can't save a large file , Split into multiple small files , Then splice it together , This is developing large-scale 、 Complex projects are very difficult .
But though JS The official does not give modularity , But the developer community has given birth to several modular ideas , Or specification , Such as AMD、CMD、CommonJS etc. .
In these specifications ,AMD and CMD Modularization can be realized on the browser side ,CommonJS More suitable for Node End , Because it cannot solve the asynchronous loading problem on the browser side by default , Therefore, it is not suitable for browser side .
2015 year , stay ES6 At the time of release ,JS The government finally gave its own Modular Scheme ES6 Module(ESM), This modular scheme can replace 、AMD、CMD and CommonJS, Support at the same time Modularization of browser and server , Finally achieve a unified modular scheme .
No matter what kind of modular solution , All support some basic concepts
- One js File is a module
- The data in the module is independent , Other modules cannot be accessed by default
Let's focus on CommonJS and ESM
2. CommonJS
This modular specification is similar to node.js Accompany each other , For a long time ,Node Only this modular scheme is supported .
Use exports perhaps module.exports Burst data , Use require Load module
a.js export
let name='yhb'
module.exports.name=name
let age=20
module.exports.age=age
b.js Import
const a_modue=require('./a')
console.log(a_modue)
a_module It's an object
{ name: 'yhb', age: 20 }
a.js You can also export data in the following way
let name = 'yhb'
let age = 20
module.exports = {
name,
age
}
Above is CommonJS The most basic content of modularity
3. ES6 modularization
ES6 Modular abbreviation ESM
ESM Burst data usage export perhaps export default, Load data using import
It also supports both server and browser
3.1 node Use in ESM
node If you want to change to use ESM, Need to be in package.json Add type attribute , And set the value to module(type The default value of the property is 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 You can also export to the following
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 When loading modules in , Except like above , Specify the data in the module to be loaded separately , The whole module can also be loaded
import * as a_module from './a.js'
console.log(a_module.name)
console.log(a_module.age)
a_module.sayHello()
In the code above , Developers need to know a.js The name of the variable or function in the module , When loading or using , The name cannot be misspelled ,
Like the following code , because a.js There is no aaa Variable , Therefore, the following introduction will report an error
import {name,aaa} from './a.js'
Is it possible to , Developers do not need to know the names of variables or functions in the module , It can also be used ?
Use... In the module export default Grammar can solve this problem well
a.js
let name='yhb'
export default name
b.js
import sss from './a.js'
console.log(sss)
export default It is equivalent to deriving a called default The variable of (default Variables and name The values of the variables are the same ),b.js You can give it a name at will
export default The exported can also be a function , Because the function can be named outside the module at will , So here we use anonymous functions , The function name has no meaning here
export default function (){
console.log('sayHello')
}
b.js
import f1 from './a.js'
f1()
because expor default Is the default output , So there can only be one in each module export default sentence
export default Except like above , Export a variable or function , You can also export an object
let name = 'yhb'
let age = 20
export default {
name,
age
}
When other modules are loaded , Not like not using default The way of writing time
import {name,age} from './a.js'
But it must be written like this
import a_module from './a.js'
console.log(a_module.name)
console.log(a_module.age)
3.2 The browser uses 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} This year, ${age} Year old `)
}
printInfo()
index.html
<script type="module" src="./b.js"></script>
If b.js No call in printInfo, But export
import {name,age} from './a.js'
function printInfo(){
console.log(`${name} This year, ${age} Year old `)
}
export {printInfo}
index.html If you want to call printInfo Words , It needs to be written like this
<script type="module">
import {printInfo} from './b.js'
printInfo()
</script>
边栏推荐
- 深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
- js中复选框checkbox如何判定为被选中
- hellogolang
- Xcode Revoke certificate
- 尤雨溪,来了!
- Three. JS introductory learning notes 13: animation learning
- How to implement backspace in shell
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- Logback日志框架第三方jar包 免费获取
- Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
猜你喜欢
Unity drawing plug-in = = [support the update of the original atlas]
Dotween -- ease function
Vs tool word highlight with margin
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Notification uses full resolution
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
无线传感器网络--ZigBee和6LoWPAN
Leetcode-231-2的幂
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Unity3d click events added to 3D objects in the scene
随机推荐
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
laravel构造函数和中间件执行顺序问题
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
Leetcode-136-只出现一次的数(用异或来解答)
There are many ways to realize the pause function in JS
Introduction to pyGame games
Iptables only allows the specified IP address to access the specified port
Use moviepy Editor clips videos and intercepts video clips in batches
Numpy -- epidemic data analysis case
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
A JS script can be directly put into the browser to perform operations
MySQL中, 如何查询某一天, 某一月, 某一年的数据
Three. JS introductory learning notes 15: threejs frame animation module
A link opens the applet code. After compilation, it is easy to understand
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
Logback日志框架第三方jar包 免费获取
Shader basic UV operations, translation, rotation, scaling
AE learning 01: AE complete project summary
Mysql database backup script