nodejs 中模擬實(shí)現(xiàn) emmiter 自定義事件
更新時(shí)間:2016年02月22日 11:15:00 投稿:hebedich
這篇文章主要介紹了Nodejs中自定義事件實(shí)例,比較簡(jiǎn)單的一個(gè)例子,需要的朋友可以參考下。
nodejs 中模擬實(shí)現(xiàn) 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>
您可能感興趣的文章:
相關(guān)文章
Node.js報(bào)錯(cuò)信息Error:?Cannot?find?module?'XXX'問(wèn)題及解
這篇文章主要介紹了Node.js報(bào)錯(cuò)信息Error:?Cannot?find?module?'XXX'問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
nodejs入門(mén)教程二:創(chuàng)建一個(gè)簡(jiǎn)單應(yīng)用示例
這篇文章主要介紹了nodejs入門(mén)教程之創(chuàng)建一個(gè)簡(jiǎn)單應(yīng)用的方法,涉及nodejs http模塊的引用、端口監(jiān)聽(tīng)等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
Nodejs使用mysql2操作數(shù)據(jù)庫(kù)的方法完整講解
MySQL2是一個(gè)基于Node.js的MySQL數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序,它是MySQL官方推薦的驅(qū)動(dòng)之一,下面這篇文章主要給大家介紹了關(guān)于Nodejs使用mysql2操作數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2024-01-01
基于Alpine Linux構(gòu)建前端node-web鏡像步驟詳解
這篇文章主要為大家介紹了基于Alpine Linux構(gòu)建前端node-web鏡像步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

