Nodejs学习笔记-事件机制

代码

https://github.com/fengchunjian/nodejs_examples/tree/master/event

//models/UserBean.js
var events = require("events");

function UserBean() {
    this.eventEmit = new events.EventEmitter();
    this.zhuce = function() {
        console.log("注册成功");
        this.eventEmit.emit("zhuce", "aaa", "bbb");
    }
    this.login = function(user, pwd) {
        console.log("登录成功: " + user + ", " + pwd);
    }
}

module.exports = UserBean;
//event.js
var events = require("events");
var UserBean = require("./models/UserBean");

user = new UserBean();
user.eventEmit.once("zhuce", function(uname, pwd) {
    console.log("收到注册成功监听");
    user.login(uname, pwd);
});
user.zhuce();

参考文档

node.js教程16_事件机制
http://edu.51cto.com/center/course/lesson/index?id=124542
nodejs_事件(n16_event)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=50

你可能感兴趣的:(Nodejs学习笔记-事件机制)