当前位置:网站首页>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
边栏推荐
- 2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
- What does software testing do? Find defects and improve the quality of software
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
- Pytest multi process / multi thread execution test case
- 【微服务|openfeign】@FeignClient详解
- Leetcode skimming: binary tree 07 (maximum depth of binary tree)
- 透过JVM-SANDBOX源码,了解字节码增强技术原理
- Rhcsa-- day one
- hbuildx中夜神模拟器的配置以及热更新
- How was my life in 2021
猜你喜欢

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

Flink学习8:数据的一致性

Perf simple process for multithreaded profile

Msgraphmailbag - search only driveitems of file types

Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)

R语言dplyr中的Select函数变量列名

leetcode刷题:二叉树06(对称二叉树)

Idea configuration 360zip open by default -- external tools

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

idea修改主体颜色
随机推荐
JS实现文字滚动 跑马灯效果
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Perf simple process for multithreaded profile
Configuration and hot update of nocturnal simulator in hbuildx
Leetcode brush question: binary tree 06 (symmetric binary tree)
(指针)自己写一个比较字符串大小的函数,功能与strcmp类似。
一位毕业生的自我分享
Huawei cloud Kunpeng engineer training (Guangxi University)
Redis:哈希hash类型数据操作命令
华为云鲲鹏工程师培训(广西大学)
ctf-pikachu-XSS
My opinion on how to effectively telecommute | community essay solicitation
ctf-pikachu-CSRF
Small record of thinking
RHCSA 04 - 进程管理
软件测试是干什么的 发现缺陷错误,提高软件的质量
Getting started with the go language is simple: go implements the Caesar password
毕业三年,远程半年 | 社区征文
10 reasons for not choosing to use free virtual hosts
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer