当前位置:网站首页>Modular specifications
Modular specifications
2022-07-31 08:33:00 【Aviator_huahua】
模块化规范
模块化概述
Modularization refers to dividing a complex program,按照一定的规则(规范)Split into several chunks(文件),并组合在一起.块的内部数据/实现是私有的,只是向外部暴露一些接口(方法)与外部其他模块通信.
简单来说,is a complex program,按照功能(or other rules)划分成不同的模块.Implement the function of the current module in each module,Implemented procedural externals are not concerned,You only need to expose the interface that implements the function to the outside world.
为什么要模块化
- 降低程序复杂度
- 降低程序耦合度
- 便于部署(Can be imported by module,Unused modules can not be imported or deployed)
模块化优点
- 避免命名冲突(减少命名空间污染)
- 更好的分离,按需加载
- 更高的复用性
- 高可维护性
模块化面临的问题
- 请求多
- 依赖模糊
- 难以维护
模块化演变
全局function模式
//module1.js let msg='hello' function f1(){ console.log('module1.f1',msg) } //index.js <script src="./module1.js"></script>namespace命名空间模式
let module2={ msg:'hello', f1(){ console.log('module2.f1',this.msg) } }IIFE模式
(function (window) { let msg = 'hello' function f1() { console.log('module3', msg); } window.module3 = { f1, msg } })(window)
模块化规范
1、CommonJS
说明
- Every file can be treated as a module
- 在服务端 The loading of modules is run-time synchronous loading
- 在浏览器端 模块需要提前编译打包处理
基本语法
暴露模块
方法一:module.exports = value 方法二:exports.xxx = value Essentially exposedexports对象引入模块
require('...') 第三方模块:xxx为模块名 自定义模块:xxx为模块文件路径
使用
// module1.js
let msg = 'module1'
module.exports = {
msg,
f1() {
console.log('module1 print', this.msg);//It will be lost when it is deconstructed and called directlythis
}
}
// module2.js
module.exports=function f2(){
console.log('module2 print');
}
// module3.js
exports.foo=function(){
console.log('module3-foo print');
}
exports.bar=function(){
console.log('module3-bar print');
}
// app.js
let module1 = require('./modules/module1')
let f2 = require('./modules/module2')
let {
foo:f3, //重命名
bar
} = require('./modules/module3')
console.log('=======01=========');
console.log('01-module1 require:', module1.msg);
module1.f1();
console.log('=======02=========');
f2();
console.log('=======03=========');
f3();
bar();
2、AMD
说明
- 专门用于浏览器端,模块的加载是异步的
- 依赖于require.js,需要安装依赖,It can be used after configuration
基本语法
暴露模块
// 定义没有依赖的模块 define(function(){ return 模块 }) // 定义有依赖的模块 define(['module1','module2'],function(m1,m2){ return 模块 })引入模块
require(['module1','module2'],function(m1,m2){ 使用m1/m2 })
使用
// dataService.js 定义没有依赖的模块
define(function(){
let name = 'dataService.js';
function getName(){
return name
}
// 暴露模块
return {getName};
})
// alerter.js 定义有依赖的模块
define(['dataService'], function(dataService){
let msg = 'alerter.js;
function showMsg(){
console.log(msg, dataService.getName());
}
return {showMsg};
})
// main.js Import the module and configure the path
(function (){
// 配置
requirejs.config({
// baseUrl: 'js/lib', //基本路径
paths: { // 配置路径,默认会添加js文件后缀,Don't add it yourself
dataService: './modules/dataService',
alerter: './modules/alerter'
}
})
requirejs(['alerter'], function(alerter){
alerter.showMsg();
})
})()
3、CMD
说明
- 专门用于浏览器端,模块的加载是异步的
- 模块使用时才会加载执行
- 依赖Sea.js
基本语法
暴露模块
// 定义没有依赖的模块 define(function(require,exports,module){ exports.xxx=value module.exports=value }) // 定义有依赖的模块 define(function(require,exports,module){ // 引入依赖模块(同步) var module2=require('./module2') // 引入依赖模块(异步) require.async('./module3',function(m3){ }) // 暴露模块 exports.xxx=value })引入模块
define(function(require){ var m1 = require('./module1') var m4 = require('./module4') m1.show() m4.show() })
4、ES6
说明
- 依赖模块需要编译打包处理
基本语法
暴露模块
// 分别暴露 export function foo(){ console.log('foo') } export function bar(){ console.log('bar') } export let arr = [1,2,3] // 统一暴露 function fun(){ console.log('fun') } function fun2(){ console.log('fun2') } let arr = [1,2,3] export {fun, fun2, arr} // 默认暴露 Any type of data can be exposed,暴露什么数据,接收到的就是什么数据 export default () => { console.log('default') } 或 export default [1,2,3] 或 let arr = [1,2,3] export default { foo(){ console.log('foo') }, arr }引入模块
import xxx from '...'
使用
// module1.js
export function f1(){
console.log('f1')
}
// module2.js
export let a = 1
// module3.js
let arr = [1,2,3]
function f3(){
console.log('f3')
}
export {arr, f3}
// module4.js
export default function f4(){
console.log('f4')
}
// index.js
import {f1} from './js/module1'
import {a} from './js/module2'
import {arr, f3} from './js/module3'
import f4 from './js/module4'
边栏推荐
- Flutter Paystack implements all options
- 模块化规范
- 开源|商品识别推荐系统
- [Yellow ah code] Introduction to MySQL - 3. I use select, the boss directly drives me to take the train home, and I still buy a station ticket
- vscode输出中文乱码问题
- The torch distributed training
- 【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
- Reimbursement Process | By Tianfang
- 哪些字符串会被FastJson解析为null呢
猜你喜欢

中软国际携手深开鸿发布(1+1) x N 战略,以数字化、智慧化改变人类生产和生活方式

Open Source | Commodity Recognition Recommender System

Docker-compose安装mysql

MySql database optimization query tool

"C language game" entry-level chess game (robot enhanced version)

Vscode: Project-tree plugin

MySQL 8.0.29 解压版安装教程(亲测有效)

SSM框架讲解(史上最详细的文章)

Vue项目通过node连接MySQL数据库并实现增删改查操作

【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
随机推荐
PowerCLi 一键批量部署OVA 到esxi 7
Golang-based swagger super intimate and super detailed usage guide [there are many pits]
ZCMU--1862: zbj的狼人杀
射频电路学习之滤波电路
模块化规范
How to Install MySQL on Linux
Open Source | Commodity Recognition Recommender System
Linux redis6.2.6 configuration file
Visual Studio新功能出炉:低优先级构建
0730~Mysql optimization
0730~Mysql优化
[MySQL exercises] Chapter 2 Basic operations of databases and data tables
Practical Bioinformatics 2: Multi-omics data integration and mining
会话技术之Coookie && Session详解
MySql database optimization query tool
PHP中 比较 0、false、null,‘‘ “
一、MySQL主从复制原理
The first part of the R language
哆啦a梦教你页面的转发与重定向
【MySQL中auto_increment有什么作用?】