博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node中事件(events)模块一些用法和原理
阅读量:7054 次
发布时间:2019-06-28

本文共 8067 字,大约阅读时间需要 26 分钟。

//node中的事件(evets)模块的用法和原理实现

//1、基本用法on、emit let EventEmitter=require("./events"); function Office(){

}; let util=require("util"); util.inherits(Office,EventEmitter)//相当于Office.prototype.proto= EventEmitter.prototype; let office = new Office(); office.on("paper",user1); function user1(){ console.log("user1订阅了报纸") } office.on("paper",user2); function user2(){ console.log("user2订阅了报纸") } office.emit("paper"); //user1订阅了报纸 //user2订阅了报纸

//1实现的原理.on和emit的实现原理和发布订阅一样,用一个空的对象的key存储用户订阅的内容,value存储订阅此内容的用户{"paper":[user1,user2]}

function EventEmitter(){ this._events=Object.create(null);//{}中有原型链,上面创建的是没有原型链的对象 } EventEmitter.prototype.on=function(type,callback){//对象存储订阅的内容 if(!this._events){ this._events=Object.create(null);//原型指向某函数时,让这个函数中也有这个对象 } if(this._events[type]){ this._events[type].push(callback);//有paper说明之前有用户,有用户说明有数组,直接push }else{ this._events[type]=[callback];//第一个用户用数组存起来 } } EventEmitter.prototype.emit=function(type){//遍历对象中存储的方法 if(this._events[type]){//只有订阅这个事件才执行 this._events[type].forEach(listener =>{ listener.call(this); }) } } module.exports=EventEmitter;

//2、带参数的on、emit

let EventEmitter=require("events"); function Office(){

}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息");

//user1报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息

//2.实现的原理

function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){ if(!this._events){ this._events=Object.create(null); } if(this._events[type]){ this._events[type].push(callback); }else{ this._events[type]=[callback]; } } EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag);//on中的函数上添加参数 }) } } module.exports=EventEmitter;

//3、removeListener删除

let EventEmitter=require("./events"); function Office(){

}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息"); office.removeListener("paper",user1); office.emit("paper","这里是报社发出的消息2"); //user1报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息2

//3.removeListeren实现的原理:找到相应的数组项删除

function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){ if(!this._events){ this._events=Object.create(null); } if(this._events[type]){ this._events[type].push(callback); }else{ this._events[type]=[callback]; } } EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){//有此事件才进入 this._events[type]=this._events[type].filter(function(listener){ return listener != callback;//只有不和需要移除的函数相等才返回 }) } } EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag);//on中的函数上添加参数 }) } } module.exports=EventEmitter;

//4、once只订阅一次

let EventEmitter=require("./events"); function Office(){

}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.once("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息"); office.emit("paper","这里是报社发出的消息2");

//user1报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息2

//4.once实现的原理:实现一次后清空

function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){ if(!this._events){ this._events=Object.create(null); } if(this._events[type]){ this._events[type].push(callback); }else{ this._events[type]=[callback]; } } EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){ this._events[type]=this._events[type].filter(function(listener){ return listener != callback && listener.l !==callback;//删除时把有l的也删除 }) } } EventEmitter.prototype.once=function(type,callback,flag){ function wrap(){//确保这个在on之后执行 callback(...arguments)//argument是回调函数的参数 this.removeListener(type,wrap);//数组中移除掉 } wrap.l=callback//避免removeListener删除不了 this.on(type,wrap,flag);//先执行一次 } EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag);//on中的函数上添加参数 }) } } module.exports=EventEmitter;

//5.addListener用法同on

//5的原理

EventEmitter.prototype.addListener=EventEmitter.prototype.on;//将on的原型指向addlistener

//6.newListener监听之后的事件

let EventEmitter=require("./events"); function Office(){

}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("newListener",function(data){ console.log(data) }) office.on("paper1",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息"); //paper1 //user1报纸:这里是报社发出的消息

//6.newListener实现的原理:排除在newlistener之前和自己本身的事件,将事件作为参数传给回调函数

function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){

if(!this._events){    this._events=Object.create(null);}if(type !=="newListener"){//排除type为newListener的情况    this._events["newListener"] //排除type在newListener之前的情况    && this._events["newListener"].forEach(listener =>{        listener(type)//将事件作为参数传出去    })}if(this._events[type]){    this._events[type].push(callback);}else{    this._events[type]=[callback];}复制代码

} EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){ this._events[type]=this._events[type].filter(function(listener){ return listener != callback && listener !==callback; }) } } EventEmitter.prototype.once=function(type,callback,flag){ function wrap(){ callback(...arguments) this.removeListener(type,wrap); } wrap.l=callback; this.on(type,wrap,flag); } EventEmitter.prototype.addListener=EventEmitter.prototype.on;//将on的原型指向addlistener EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag); }) } } module.exports=EventEmitter;

//7.defaultMaxListeners默认事件的最大值

let EventEmitter=require("./events"); console.log(EventEmitter.defaultMaxListeners)

//10

//8.listener监听事件

let EventEmitter=require("./events"); function Office(){

}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } console.log(office.listeners('paper')) //[ [Function: user1], [Function: user2] ]

//8.listener实现的原理里:对象中找到指定的事件,将事件下的数组返回

function EventEmitter(){ this._events=Object.create(null); } EventEmitter.defaultMaxListeners="10" EventEmitter.prototype.on=function(type,callback){

if(!this._events){    this._events=Object.create(null);}if(type !=="newListener"){    this._events["newListener"]    && this._events["newListener"].forEach(listener =>{        listener(type)    })}if(this._events[type]){    this._events[type].push(callback);}else{    this._events[type]=[callback];}复制代码

} EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){ this._events[type]=this._events[type].filter(function(listener){ return listener != callback && listener !==callback; }) } } EventEmitter.prototype.listeners=function(type){ return this._events[type];//返回这个事件下的数组 } EventEmitter.prototype.once=function(type,callback,flag){ function wrap(){ callback(...arguments) this.removeListener(type,wrap); } wrap.l=callback; this.on(type,wrap,flag); } EventEmitter.prototype.addListener=EventEmitter.prototype.on;//将on的原型指向addlistener EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag); }) } } module.exports=EventEmitter;

转载地址:http://bllol.baihongyu.com/

你可能感兴趣的文章
如何关闭Microsoft OutLook 2013 Search功能
查看>>
Android倒计时 Android仿京东倒计时 android电商app源码倒计时源码
查看>>
教你一个查找下载缓存文件的自带方法
查看>>
单点故障的解决方案
查看>>
SQL Server 默认跟踪应用2 -- 审核备份和恢复事件
查看>>
RHEL5 安装VMware tools
查看>>
paramiko分开执行多条命令 不像之前一样使用\n
查看>>
NO.2 身为IT人不可不知的技能点
查看>>
Application Center Test知识点滴积累
查看>>
MySQL多表更新
查看>>
Docker基于已有的镜像制新的镜像-Docker for Web Developers(3)
查看>>
不需手动锁表同步mysql数据库
查看>>
Exchange企业实战技巧(18)日志规则应用之审计邮箱
查看>>
H3C交换机基本ACL配置
查看>>
redis-dump-load数据导出导入备份工具使用介绍
查看>>
dropbox连接不上解决方法
查看>>
Jbuilder9 + WebLogic8.1的基本配置
查看>>
01-Windows Server 2012 R2 远程桌面服务部署指南
查看>>
实例讲解遗传算法——基于遗传算法的自动组卷系统【理论篇】
查看>>
烂泥:puppet添加带密码的用户
查看>>