当前位置:网站首页>模块化笔记
模块化笔记
2022-06-11 06:56:00 【煮酒cos】
文章目录
模块化笔记
modular
全局function模式
- 能将不同的功能封装成不同的全局函数
- 但是容易污染
namespace模式
简单的对象封装
let obj = { message :'module2' foo(){ console.log('foo()',this.message) } }但是还是容易被修改
IIFE模式
用闭包函数进行的包装,还有立即执行函数
(function(window){ let message ='moudle' function foo(){ console.log('foo()',message) } window.moudle = { foo} //绑定在全局的一个对象上 })(window)调用直接用moule.foo()采用即可
IIFE模式增强
如何增强:引入依赖
这就是现代模块化实现的基石
(function(window,$){
let message ='moudle'
function foo(){
console.log('foo()',message)
}
window.moudle = {foo}
//绑定在全局的一个对象上
$('body').css('background','red')
})(window,jquery)
load script
- 一个页面需要引入多个js文件
- 问题
- 请求过多
- 依赖模糊
- 难以维护
- 这些问题可以通过现代化模块化编程和项目构建来解决
- commonjs
commonjs
- 规范
- 说明
- 网址。。。。。
- 每个文件都可以当作一个模块
- 在服务器端:模块的加载时运行时同步加载的(会等待,相当于阻塞
- 在浏览器端:模块需要提前编译打包
- 基本语法
- 暴露模块
- exports.xxx = value(任意数据类型)
- moudle.exports = value
- 暴露的模块到底是什么?
- 都是exports对象
- 原本时{}空的对象
- 第一个时给exports加属性,第二个时给模块的exports对象赋值
- 引入模块
- require(xxx)
- 第三方模块:xxx为模块名(就是npm的包名)
- 自定义模块:xxx为模块文件路径名
- 暴露模块
- 说明
- 实现
- 服务器端的实现
- node.js
- http://nodejs.cn/
- 浏览器端的实现
- Browserify(魔法帽(很神奇
- http://browserify.org/
- 也称commonjs的浏览器端的打包工具
- 区别Node和Browserify
- 服务器端的实现
//package.json
{
//必须有两个属性
'name':'commonjs_node',
//不能用中文,不能用大写
'version':'1.0.0'
}
//用终端生成:npm init
//在后面可以自己写一个替代
commonjs-Node
在node环境下运行
下载第三方模块 npm install uniq(–save为默认属性
moudle1.exports = { msg:'moudle1' foo(){ console.log(this.msg) } }moudle2.exports = function(){ console.log(moudle) }exports.foo = function(){ consloe.log('foo() moudles3') } exports.bar = function(){ consloe.log('bar() moudles3') } //继续暴露 exports.arr=[1,2,2,3,3]//引入第三方模块,放在自定义库的上面(默认) let uniq = require('uniq') //将其他的模块引入 let moudle1 = require('./moudle1') let moudle2 = require('./moudle2') let moudle3 = require('./moudle3') moudle1.foo(); moudle2()//moudle2相当于函数 moudle3.foo() moudle3.bar() let result = uniq(moudle3.arr) console.log(result) //uniq根据数组的第一位的数字惊醒的编码
commonjs-Browserify
在index.html环境下运行
dist/build(构建的意思) 打包生成文件的目录

下载browserify
全局npm install browserify -g 局部npm install browserify --save -dev //运行依赖和开发依赖开发依赖 -dev (devdependencies)
运行依赖 --save(生产环境)(dependencies)
moudle1.exports = { msg:'moudle1' foo(){ console.log(this.msg) } }moudle2.exports = function(){ console.log(moudle) }exports.foo = function(){ consloe.log('foo() moudles3') } exports.bar = function(){ consloe.log('bar() moudles3') }let moudle1 = require('./moudle1') let moudle2 = require('./moudle2') let moudle3 = require('./moudle3') moudle1.foo(); moudle2()//moudle2相当于函数 moudle3.foo() moudle3.bar()//在index.html下引入,但要对源文件进行打包处理(用browserify
-o的意思为输出的意思
左侧是源文件
右侧是输出的文件处,bundle意思是打包,其实怎么处理文件名都无所谓,但最好这样
AMD
即requirejs的基本使用方法
模块包的引入(文件创建太多,没法写了)
(显示声明依赖注入
CMD
省略
ES6
最火,所以重点
边栏推荐
- This comprehensive understanding
- . Net C Foundation (6): namespace - scope with name
- VTK-vtkPlane和vtkCutter使用
- 1266_ Implementation analysis of FreeRTOS scheduler startup code
- Flutter Container组件
- Simple integration of client go gin six list watch two (about the improvement of RS, pod and deployment)
- Object. Specific implementation and difference between create() and new
- Shuttle inside and outside margins
- client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)
- SQL language - query statement
猜你喜欢

Redux learning (III) -- using Redux saga, writing middleware functions, and splitting reducer files

Whether the ZABBIX monitoring host is online

品牌定位个性六种形态及结论的重大意义

搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?

Redux learning (I) -- the process of using Redux
![Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]](/img/0d/2de3413fcb77ac2bd093677035f2e7.jpg)
Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]
![[]==! []](/img/65/ab724c74b080da319ed5c01c93fdb7.png)
[]==! []

Flutter 约束容器组件

Leetcode hot topic 100 topic 21-25 solution

关于 QtCreator的设计器QtDesigner完全无法正常拽托控件 的解决方法
随机推荐
Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?
Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
Flutter Container组件
Biweekly investment and financial report: capital rush yuan universe game
Starting from scratch (I)
[MATLAB image encryption and decryption] chaotic sequence image encryption and decryption (including correlation test) [including GUI source code 1862]
Dynamic import
Leetcode hot topic 100 topic 11-15 solution
socket. IO cross domain stepping pit
The difference between TCP and UDP
RGB-D Salient Object Detection withCross-Modality Modulation and Selection
About daily report plan
100. same tree
Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
Open source cartoon server mango
Flutter 约束容器组件
617. merge binary tree
迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)
Zabbix 监控主机是否在线
网狐游戏服务器房间配置向导服务定制功能页实现