当前位置:网站首页>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
边栏推荐
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- JDBC advanced
- RHCSA 04 - 进程管理
- (指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
- 量子力学习题
- Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
- Leetcode skimming: binary tree 09 (minimum depth of binary tree)
- hbuildx中夜神模拟器的配置以及热更新
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- 拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
猜你喜欢

【CSRF-01】跨站请求伪造漏洞基础原理及攻防

Flink learning 7: application structure

三年进账35.31亿,这个江西老表要IPO了

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

Confession code collection, who says program apes don't understand romance

Katalon中控件的参数化

The difference between bagging and boosting in machine learning

LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)

如何远程办公更有效率 | 社区征文

DP83848+网线热拔插
随机推荐
leetcode刷题:二叉树06(对称二叉树)
Redis cluster view the slots of each node
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
02 ls 命令的具体实现
R语言dplyr中的Select函数变量列名
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
2020 Bioinformatics | TransformerCPI
The new data center helps speed up the construction of a digital economy with data as a key element
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
VIM mapping command
Tcp- simple understanding of three handshakes and four waves
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
JS realizes the effect of text scrolling marquee
How was my life in 2021
PostgreSQL users cannot create table configurations by themselves
VIM add interval annotation correctly
I Build a simple microservice project
【读书会第十三期】视频文件的封装格式
Flink学习7:应用程序结构
C language bidirectional linked list first edition