当前位置:网站首页>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
边栏推荐
- Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
- 如何远程办公更有效率 | 社区征文
- 线程常用的方法
- Smart subway | cloud computing injects wisdom into urban subway transportation
- Exercises in quantum mechanics
- [csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
- PostgreSQL users cannot create table configurations by themselves
- Katalon uses script to query list size
- PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?
- (指针)编写函数void fun(int x,int *pp,int *n)
猜你喜欢
leetcode刷题:二叉树09(二叉树的最小深度)
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
Graduation project: design seckill e-commerce system
Tcp- simple understanding of three handshakes and four waves
ctf-pikachu-XSS
Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
指针数组和数组指针
Parameterization of controls in katalon
【罗技】m720
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
随机推荐
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
[microservice openfeign] @feignclient detailed explanation
laravel admin里百度编辑器自定义路径和文件名
Getting started with the go language is simple: go implements the Caesar password
Storage of MySQL database
RHCSA 07 - 用户与群组管理
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
JS realizes the effect of text scrolling marquee
ctf-pikachu-XSS
Flink learning 6: programming model
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
Flink学习7:应用程序结构
leetcode刷题:二叉树08(N叉树的最大深度)
How was my life in 2021
(pointer) write a function to compare the size of strings by yourself, which is similar to StrCmp.
10 reasons for not choosing to use free virtual hosts
Rhcsa-- day one
Brief explanation of depth first search (with basic questions)
[book club issue 13] multimedia processing tool ffmpeg tool set