当前位置:网站首页>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
边栏推荐
- How to dynamically cache components in Vue multi-level route nesting
- Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
- 【webrtc】m98 ninja 构建和编译指令
- Pytest multi process / multi thread execution test case
- How to telecommute more efficiently | community essay solicitation
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
- Exercises in quantum mechanics
- 毕业设计:设计秒杀电商系统
- JS realizes the effect of text scrolling marquee
猜你喜欢

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

Flink learning 6: programming model

2020 Bioinformatics | TransformerCPI

Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡

毕业设计:设计秒杀电商系统

Storage of MySQL database

软件测试是干什么的 发现缺陷错误,提高软件的质量
![[Logitech] m720](/img/bb/44144a1c3907808398c05b3b36962c.png)
[Logitech] m720

北漂程序员,月薪20K,一年攒15W,正常吗?

1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
随机推荐
Tcp- simple understanding of three handshakes and four waves
Getting started with the go language is simple: go implements the Caesar password
Three years of graduation, half a year of distance | community essay solicitation
STM32 external DHT11 display temperature and humidity
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Leetcode brush questions: binary tree 05 (flip binary tree)
Smart subway | cloud computing injects wisdom into urban subway transportation
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
vim正确加区间注释
dried food! Generation of rare samples based on GaN
普源DS1000Z系列数字示波器在通信原理实验中的应用方案
Leetcode skimming: binary tree 07 (maximum depth of binary tree)
Redis:哈希hash类型数据操作命令
96% of the collected traffic is prevented by bubble mart of cloud hosting
(pointer) write a function to compare the size of strings by yourself, which is similar to StrCmp.
02 ls 命令的具体实现
01 qemu 启动编译好的镜像 VFS: Unable to mount root fs on unknown-block(0,0)
Restore the subtlety of window position
Flink learning 7: application structure