当前位置:网站首页>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'
边栏推荐
- 【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
- 【C#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)
- 0730~Mysql optimization
- "C language game" entry-level chess game (robot enhanced version)
- 普通函数的参数校验
- SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
- 日志导致线程Block的这些坑,你不得不防
- How to upgrade nodejs version
- 深度理解递归,手撕经典递归问题(汉诺塔,青蛙跳台阶),保姆级教学。
- 48页智慧城市规划蓝图 解决方案
猜你喜欢

【MySQL中auto_increment有什么作用?】

48页智慧城市规划蓝图 解决方案

全国中职网络安全B模块之国赛题远程代码执行渗透测试 PHPstudy的后门漏洞分析
![[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](/img/7b/f50c5f4b16a376273ba8cd27543676.png)
[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

SQL 嵌套 N 层太长太难写怎么办?

射频电路学习之滤波电路

SQL statement knowledge

PowerCLi 一键批量部署OVA 到esxi 7

LED flashing on CY7C68013A

A, MySQL principle of master-slave replication
随机推荐
数组every和some方法的区别?
7/28-7/29 期望+思维+后缀数组+ST表
期刊会议排名、信息检索网站推荐以及IEEE Latex模板下载
全国中职网络安全B模块之国赛题远程代码执行渗透测试 PHPstudy的后门漏洞分析
Locust 1.0版本引入的变化
Open Source | Commodity Recognition Recommender System
First acquaintance with NK-RTU980 development board
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Middle)--Search Suggestions
如何在 Linux 上安装 MySQL
【面试题】从输入URL到游览器渲染完成,经历了什么
C语言三子棋(井字棋)小游戏
深度理解递归,手撕经典递归问题(汉诺塔,青蛙跳台阶),保姆级教学。
PowerCLi 通过自建PXE Server一键esxi7下批量部署常规New-VM
Linux redis6.2.6 configuration file
The first part of the R language
Regarding "computing power", this article is worth reading
mysql insert new field method
How to Install MySQL on Linux
ScheduledExecutorService - 定时周期执行任务
SQL语句知识大全