当前位置:网站首页>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即将上线
- PostgreSQL users cannot create table configurations by themselves
- [microservice openfeign] use openfeign to remotely call the file upload interface
- The new data center helps speed up the construction of a digital economy with data as a key element
- The difference between bagging and boosting in machine learning
- Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
- 如何远程办公更有效率 | 社区征文
- Idea configuration 360zip open by default -- external tools
- Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
- Graduation project: design seckill e-commerce system
猜你喜欢
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
还原窗口位置的微妙之处
Idea configuration 360zip open by default -- external tools
指针数组和数组指针
统计遗传学:第三章,群体遗传
北漂程序员,月薪20K,一年攒15W,正常吗?
Flink learning 7: application structure
10 reasons for not choosing to use free virtual hosts
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Parameterization of controls in katalon
随机推荐
分布式系统:what、why、how
Flink learning 6: programming model
Spa in SDP
TCP-三次握手和四次挥手简单理解
How to add custom API objects in kubernetes (1)
Idea configuration 360zip open by default -- external tools
Flink学习6:编程模型
软件测试是干什么的 发现缺陷错误,提高软件的质量
[microservice openfeign] use openfeign to remotely call the file upload interface
leetcode刷题:二叉树08(N叉树的最大深度)
Flink learning 8: data consistency
统计遗传学:第三章,群体遗传
RHCSA 07 - 用户与群组管理
毕业三年,远程半年 | 社区征文
How to add custom API objects in kubernetes (1)
Restore the subtlety of window position
【罗技】m720
思考的小记录
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
批处理初识