Node.JS事件驱动机制
1.事件驱动程序绑定事件及事件的处理程序
eventEmitter.on('eventName', eventHandler);
我们可以通过程序触发事件
// 触发事件 eventEmitter.emit('eventName');
2.实例
var EventEmitter = require('events').EventEmitter var a = new EventEmitter; //在Node.js中如何注册一个事件 a.on('myevent', function () { console.log('event called......') }) process.nextTick(function () { a.emit('myevent') }) console.log('oooooooo')
3.让类都具有事件的功能
app.js
var MyClass = require('./Test') var aa = new MyClass aa.on('fsfdfs',function(){ })
Test.js
var EventEmitter = process.EventEmitter var MyClass = function () { ///........ } MyClass.prototype.__proto__ = EventEmitter.prototype
相关推荐
sssdssxss 2020-09-22
dailinqing 2020-07-18
Noseparte 2020-04-15
seekerhit 2020-01-24
范群松 2019-12-22
gd0 2019-11-04
frank0 2019-10-12
longdan0 2013-12-02
bangrenzhuce 2019-06-29
meleto 2013-07-11
xuesong 2019-06-28
堆码时刻 2012-04-24
静 2019-06-28
lieyeleaves 2019-06-26
adeyi 2019-06-21
袅袅青烟散 2019-06-20