当前位置:网站首页>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>
边栏推荐
- 过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 通知Notification使用全解析
- Performance measure of classification model
- Notification uses full resolution
- 持续创作,还得靠它!
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
- laravel构造函数和中间件执行顺序问题
- 航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
- 【知识小结】PHP使用svn笔记总结
猜你喜欢
Description of vs common shortcut keys
MySQL数据库基本操作-DQL-基本查询
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
Logback日志框架第三方jar包 免费获取
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
C4D learning notes 1- animation - animation key frames
Xcode Revoke certificate
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
Odoo集成Plausible埋码监控平台
Statistical learning method -- perceptron
随机推荐
laravel post提交数据时显示异常
Numpy --- basic learning notes
Odoo integrated plausible embedded code monitoring platform
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
分步式监控平台zabbix
nodejs package. JSON version number ^ and~
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Application example of infinite list [uigridview]
修改配置文件后tidb无法启动
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
Laravel5.1 路由 -路由分组
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
Sysom case analysis: where is the missing memory| Dragon lizard Technology
如何在shell中实现 backspace
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
Unity3d click events added to 3D objects in the scene
神经网络c语言中的指针是怎么回事
Three. JS introduction learning notes 12: the model moves along any trajectory line
Laravel 中config的用法
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告