当前位置:网站首页>Touch and take you to implement an EventEmitter
Touch and take you to implement an EventEmitter
2022-07-04 04:22:00 【Careteen】
In the daily work of cross module development , A very common scene : Event communication between different modules .
The following are given ES5、ES6 Two ways of implementation .
ES5 Realization
var events = {}
var Events = {
on: function (key, fn) {
if (typeof fn !== 'function') {
return
}
events[key] = events[key] || []
events[key].push(fn)
},
once: function (key, fn) {
if (typeof fn !== 'function' || (fn.once && fn.hasExeced)) {
return
}
fn.once = true
fn.hasExeced = false
events[key] = events[key] || []
events[key].push(fn)
},
trigger: function (key) {
var args = [].slice.call(arguments, 1)
var fnList = events[key] || []
fnList.forEach(function (item) {
if (typeof item === 'function') {
if (item.once && item.hasExeced) {
return
} else if (item.once && !item.hasExeced) {
item.hasExeced = true
item.apply(window, args)
} else {
item.apply(window, args)
}
}
})
}
}Use
ES6 Realization
subscriber
The observer
application
ultimate
For the full code, see @careteen/event-emitter
summary
边栏推荐
- JS实现文字滚动 跑马灯效果
- 【CSRF-01】跨站请求伪造漏洞基础原理及攻防
- vim正确加区间注释
- 普源DS1000Z系列数字示波器在通信原理实验中的应用方案
- 毕业设计项目
- 深度优先搜索简要讲解(附带基础题)
- Smart subway | cloud computing injects wisdom into urban subway transportation
- laravel admin里百度编辑器自定义路径和文件名
- [csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
- [book club issue 13] packaging format of video files
猜你喜欢

普源DS1000Z系列数字示波器在通信原理实验中的应用方案

Brief explanation of depth first search (with basic questions)

User defined path and file name of Baidu editor in laravel admin

Unity draws the trajectory of pinball and billiards

How to telecommute more efficiently | community essay solicitation

Evolution of MySQL database architecture

The maximum expiration time of client secret in azure ad application registration is modified to 2 years

PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?

leetcode刷题:二叉树09(二叉树的最小深度)

Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
随机推荐
Restore the subtlety of window position
Smart subway | cloud computing injects wisdom into urban subway transportation
02 ls 命令的具体实现
How to dynamically cache components in Vue multi-level route nesting
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?
[book club issue 13] multimedia processing tool ffmpeg tool set
透过JVM-SANDBOX源码,了解字节码增强技术原理
Redis:哈希hash类型数据操作命令
干货!基于GAN的稀有样本生成
STM32外接DHT11显示温湿度
Penetration practice - sqlserver empowerment
2020 Bioinformatics | TransformerCPI
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
【读书会第十三期】视频文件的封装格式
Redis cluster view the slots of each node
R语言中如何查看已安装的R包
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
Katalon framework test web (XXVI) automatic email