当前位置: 首页 > news >正文

移动商城网站开发批量外链工具

移动商城网站开发,批量外链工具,电子商务网站建设第三章答案,门户类网站建设大约多少钱关于EventBus事件总线 事件总线,实现 on, once, emit, off on, once 是注册函数,存储起来emit时找到对应的函数,执行off找到对应的函数,从对象中删除 注意 区分on和onceon绑定的事件可连续执行,除非offonce绑定的函数…

关于EventBus事件总线

  • 事件总线,实现 on, once, emit, off
    • on, once 是注册函数,存储起来
    • emit时找到对应的函数,执行
    • off找到对应的函数,从对象中删除
  • 注意
    • 区分on和once
    • on绑定的事件可连续执行,除非off
    • once绑定的函数 emit 一次即删除,也可未执行而被 off
    • 数据结构上标识出 on 和 once

实现方案1

代码实现:使用参数区分 on和once

class EventBus {/*{key1: [{fn: fn1, isOnce: false},{fn: fn2, isOnce: false},{fn: fn3, isOnce: true},],key2: []}*/private events: {[key: string]: Array<{fn: Function; isOnce: boolean}>}constructor() {this.events = {};}on(type: string, fn: Function, isOnce: boolean = false) {const events = this.events;if (events[type] == null) {events[type] = []; // 初始化 key 的 fn 数组}events[type].push({fn, isOnce});},// 这里是初步版本once_origin(type: string, fn: Function) {const events = this.events;if (events[type] == null) {events[type] = []; // 初始化 key 的 fn 数组}events[type].push({fn, isOnce: true});},once(type: string, fn: Function, isOnce: boolean = false) {this.on(type, fn, true);},off(type: string, fn?:Function) {if (!fn) {// 解绑所有 type 的函数this.events[type] = [];} else {// 解绑单个 fnconst fnList = this.events[type];if (fnList.length) {this.events[type] = fnList.filter(item.fn !== fn);}}},emit(type: string, ...args: any[]) {const fnList = this.events[type];if (fnList == null) return;// 注意this.events[type] = fnList.filter(item => {const { fn, isOnce } = item;fn(...args);// 处理once, 它执行一次就要被过滤掉if (!isOnce) return true;return false;})}
}const e = new EventBus();
function fn1(a: any, b: any) {console.log('fn1', a, b)};
function fn2(a: any, b: any) {console.log('fn2', a, b)};
function fn3(a: any, b: any) {console.log('fn3', a, b)};e.on('key1', fn1);
e.on('key1', fn2);
e.once('key1', fn3);e.emit('key1', 10, 20); // 触发 fn1, fn2, fn3
e.off('key1', fn1);
e.emit('key1', 100, 200); // 触发 fn2

实现方案2

代码实现: 拆分保存 on和once

class EventBus {private events: { [key: string]: Array<Function>} // {key1: [fn1, fn2]}private onceEvents: {[key: string]: Array<Function>} // 结构同上constructor() {this.events = {}; // 存储 onthis.onceEvents = {}; // 存储 once}// on 触发on(type: string, fn: Function) {const events = this.events;if (events[type] === null) events[type] = [];events[type].push(fn);},once(type: string, fn: Function) {const onceEvents = this.onceEvents;if (onceEvents[type] === null) onceEvents[type] = [];onceEvents[type].push(fn);},// 解绑事件off(type: string, fn: Function) {if (!fn) {// 解绑所有事件this.events[type] = [];this.onceEvents[type] = [];} else {// 解绑单个const fnList = this.events[type];const onceFnList = this.onceEvents[type];if (fnList.length) {this.events[type] = fnList.filter(curFn => curFN!== fn);}if (onceFnList.length) {this.onceEvents[type] = onceFnList.filter(curFn => curFN!== fn);}}},emit(type: string, ...args: any[]) {const fnList = this.events[type];const onceFnList = this.onceEvents[type];if (fnList.length) {fnList.forEach(f => f(...args));}if (onceFnList.length) {onceFnList.forEach(f => f(...args));// once 执行一次就删除,这里更简单,代码更简洁this.onceEvents[type] = [];}}
}// 测试用例同上,此处省略

总结

  • 区分 on, once
  • 合理的数据结构,比算法优化更有效
http://www.zhongyajixie.com/news/30365.html

相关文章:

  • 用织梦做企业网站关键词工具软件
  • 做网站潜江西安网站建设方案优化
  • 建设银行泰州分行网站视频号视频怎么看下载链接
  • 王也道长微信头像厦门seo外包平台
  • 温州市网站制作公司网站seo优化怎么做
  • 哪些园林网站可以做外链北京网站优化效果
  • 推荐做木工的视频网站信息流优化师是什么
  • 做网站哪里比较好网上店铺的推广方法有哪些
  • 中文网站建设公司百度推广一年大概需要多少钱
  • 做音乐网站多少钱知名网络软文推广平台
  • 做网站的视频教学如何对产品进行推广
  • 做网站常熟网站搜索排名优化
  • 北京好的做网站的公司哪家好十大看免费行情的软件下载
  • 汝阳网站建设爱站长
  • 代做网站名称优化电商网站
  • 为什么网站突然打不开seo是什么工作
  • 国家市场监督管理总局班子东莞百度快速排名优化
  • 零食网站建设前的市场分析网络维护
  • 58同城哈尔滨网站建设哪家网络公司比较好
  • 淘宝客网站做百度推广完善的seo网站
  • wp做图网站茂名网站建设制作
  • 衣服网站建设日程表百度一下浏览器
  • 网站建设综合实训苏州百度快照优化排名
  • 太原网站优化哪家专业seo优化关键词放多少合适
  • 交互 网站2023知名品牌营销案例100例
  • 外包 网站开发公司我要发布信息
  • 赛门仕博做网站怎么样域名注册入口
  • xml网站模板正规seo关键词排名网络公司
  • 最专业的做网站公司百度收录规则
  • 清风网站建设百度推广一个月多少钱