1 程序1
var EventEmitter = require('events').EventEmitter
var myEmitter = new EventEmitter()
// addEventListener
myEmitter.on('get', function (something) {
console.log(something + "1")
})
myEmitter.on('get', function (something) {
console.log(something + "2")
})
myEmitter.on('get', function (something) {
console.log(something + "3")
})
myEmitter.on('get', function (something) {
console.log(something + "4")
})
myEmitter.on('get', function (something) {
console.log(something + "5")
})
myEmitter.on('get', function (something) {
console.log(something + "6")
})
myEmitter.on('get', function (something) {
console.log(something + "7")
})
myEmitter.on('get', function (something) {
console.log(something + "8")
})
myEmitter.on('get', function (something) {
console.log(something + "9")
})
myEmitter.on('get', function (something) {
console.log(something + "10")
})
myEmitter.emit("get", "监听")
结果:
监听1
监听2
监听3
监听4
监听5
监听6
监听7
监听8
监听9
监听10
如果继续增加第11条监听,系统警告:Warning: Possible EventEmitter memory leak detected. 11 get listeners added. Use emitter.setMaxListeners() to increase limit,即需要setMaxListeners()方法增加监听数量。但监听数太多容易造成内存泄漏
2 程序2
var EventEmitter = require('events').EventEmitter
var myEmitter = new EventEmitter()
// 设置最大监听数(默认建议10个监听)
myEmitter.setMaxListeners(11)
// addEventListener
myEmitter.on('get', function (something) {
console.log(something + "1")
})
myEmitter.on('get', function (something) {
console.log(something + "2")
})
myEmitter.on('get', function (something) {
console.log(something + "3")
})
myEmitter.on('get', function (something) {
console.log(something + "4")
})
myEmitter.on('get', function (something) {
console.log(something + "5")
})
myEmitter.on('get', function (something) {
console.log(something + "6")
})
myEmitter.on('get', function (something) {
console.log(something + "7")
})
myEmitter.on('get', function (something) {
console.log(something + "8")
})
myEmitter.on('get', function (something) {
console.log(something + "9")
})
myEmitter.on('get', function (something) {
console.log(something + "10")
})
myEmitter.on('get', function (something) {
console.log(something + "11")
})
myEmitter.emit("get", "监听")
3 程序3 移除监听
var EventEmitter = require('events').EventEmitter
var myEmitter = new EventEmitter()
// 设置最大监听数(默认建议10个监听)
myEmitter.setMaxListeners(11)
// addEventListener
myEmitter.on('get', function (something) {
console.log(something + "1")
})
myEmitter.on('get', function (something) {
console.log(something + "2")
})
myEmitter.on('get', function (something) {
console.log(something + "3")
})
myEmitter.on('get', function (something) {
console.log(something + "4")
})
myEmitter.on('get', function (something) {
console.log(something + "5")
})
myEmitter.on('get', function (something) {
console.log(something + "6")
})
myEmitter.on('get', function (something) {
console.log(something + "7")
})
myEmitter.on('get', function (something) {
console.log(something + "8")
})
myEmitter.on('get', function (something) {
console.log(something + "9")
})
myEmitter.on('get', function (something) {
console.log(something + "10")
})
function getEmitter(something) {
console.log(something + "11")
}
myEmitter.on('get', getEmitter)
// 移除监听
myEmitter.removeListener('get',getEmitter)
myEmitter.emit("get", "监听")
结果:移除了第11个监听
myEmitter.removeAllListeners('get')
结果:移除所有get监听事件
4 获取监听数
myEmitter.listeners('get').length
// 或者
EventEmitter.listenerCount(myEmitter,'get')
结果:10