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>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号