nodejs 中模拟实现 emmiter 自定义事件
nodejs中模拟实现emmiter自定义事件
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title></title>
<script>
functionEmitter(){
this.events={};//存放事件的地方
}
Emitter.prototype.on=function(type,cb){
varevents=this.events;
events=events[type]=events[type]||[];
events.push(cb);
};
Emitter.prototype.emit=function(type){
varargs=[].slice.call(arguments,1);
varcbs=this.events[type],cb;
while(cb=cbs&&cbs.shift()){
cb.apply(this,args);
}
};
varemitter=newEmitter();
emitter.on('customevent',function(param){
alert(param);
});
emitter.on('customevent',function(){
alert(1);
});
emitter.emit('customevent','xxx');
</script>
</head>
<body>
</body>
</html>
热门推荐
10 新年祝福语大全 简短情侣
11 爸爸生日的祝福语简短
12 集体年会祝福语简短创意
13 回归岗位文案祝福语简短
14 厂庆祝福语大全简短
15 生日贺卡祝福语英文简短
16 新年顾客简短祝福语大全
17 生日祝福语 简短独特情话
18 新年祝福语简短的感谢