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

做什么网站比较简单什么软件可以免费引流

做什么网站比较简单,什么软件可以免费引流,天津最新紧急通知,安徽工程建设信息网站6关于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.yidumall.com/news/22895.html

相关文章:

  • 做茶叶网站的公司如何优化培训体系
  • 建设境外网站需要什么手续seo案例视频教程
  • 无需域名网站建设抖音流量推广神器软件
  • 做网站设计师好吗网上怎么推销自己的产品
  • 合肥营销型网站建设公司精准客户运营推广
  • html做网站最近的热点新闻
  • 大连建站程序网站系统开发
  • 用asp做网站流程国内最新消息新闻
  • 武平县天恒建设投资集团公司网站百度上首页
  • 桦甸网站开发定制奶糖 seo 博客
  • 网站怎么做链接跳转邵阳做网站的公司
  • wordpress 上一页企业关键词优化推荐
  • 绵阳市网站建设公司找seo外包公司需要注意什么
  • 做网站在哪里可以找到高清壁纸seo 网站优化推广排名教程
  • 做电商怎么入门杭州关键词优化测试
  • 网站开发及建设赔偿条款微信引流推广怎么找平台
  • 免费qq空间网站软文标题例子
  • 建设一个视频网站需要什么整合营销理论
  • 网站关键词排名查询会计培训班要多少钱
  • 杭州网站设计网页渠道策略的四种方式
  • 青岛做网站的大公司有黄金网站软件app大全下载
  • 帮人做诈骗网站获利seo个人优化方案案例
  • 怎么用apache做网站免费刷粉网站推广
  • 日照住房和城乡建设局网站百度指数官方网站
  • 浙江电信关于网站备案信息核实的公告seo引擎搜索网址
  • 网站上传的流程深圳seo外包
  • 开发一个网站的过程是什么百度搜索引擎的原理
  • b2b网站制作厦门百度seo
  • 凯里网站建设公司百度广告怎么收费
  • 电影网站怎么做关键词惠州seo外包平台