分类分类
更新时间:2026-08-08 19:21:12作者:fang
nodejs 中模拟实现 emmiter 自定义事件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function Emitter() {
this.events = {}; //存放事件的地方
}
Emitter.prototype.on = function(type, cb) {
var events = this.events;
events = events[type] = events[type] || [];
events.push(cb);
};
Emitter.prototype.emit = function(type) {
var args = [].slice.call(arguments, 1);
var cbs = this.events[type], cb;
while (cb = cbs && cbs.shift()) {
cb.apply(this, args);
}
};
var emitter = new Emitter();
emitter.on('customevent', function(param) {
alert(param);
});
emitter.on('customevent', function() {
alert(1);
});
emitter.emit('customevent', 'xxx');
</script>
</head>
<body>
</body>
</html>
以上就是本文章的内容,希望对大家有所帮助
相关
星球小队策略游戏316.49 MBv1.15.366.638912026-08-08
下载超市大亨3D经营养成253.14 MBv10.252026-08-08
下载绝世武林角色扮演453.09 MBv1.8.14162026-08-08
下载Crush Crush手机版经营养成124.86 Mv0.4482026-08-08
下载消消大亨经营养成13.56 Mv1.0.02026-08-08
下载endacopia手机版冒险游戏1.43Gv1.0.02026-08-08
下载最强帮主角色扮演143.91 MBv9991.12026-08-08
下载战西游手游折扣版策略游戏13.77 MBv1.02026-08-08
下载富豪物语创业时代官方版经营养成215.01 Mv3.0.12026-08-08
下载咒术回战爱憎乱斗手游动作射击291.7 Mv1.0.02026-08-08
下载三国吧兄弟折扣版策略游戏380.04 MBv1.0.02026-08-08
下载代号97动作射击277.47 Mv1.0.72026-08-08
下载










