国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

node.js - nodejs removeListener 無法得到 listener
PHP中文網(wǎng)
PHP中文網(wǎng) 2017-04-17 11:31:13
0
1
1101
event.on("newListener", function (EventName, callback) {
    callback({on: EventName});
});
event.addListener("aaa", function (value) {
    console.log("我是注冊時自動觸發(fā)的" + value.on + "事件!");
});// 正常輸出:我是注冊時自動觸發(fā)的 aaa 事件!

event.on("removeListener", function (EventName, callback) {
    callback({EventName: EventName});
});
var callbackName = function (value) {
    console.log("我是刪除事件時自動觸發(fā)的" + value.EventName + "事件!");
}
event.on("ddd", callbackName);
event.removeListener("ddd", callbackName);
// 報錯!event.js:76 callback({EventName: EventName});
PHP中文網(wǎng)
PHP中文網(wǎng)

認證高級PHP講師

reply all(1)
Peter_Zhu

You should post the shutdown word! ! ! ! ! ! ! ! !

It made me check the documents myself, complete the code, and then just because of the one and only important sentence that you refused to send:

TypeError: undefined is not a function

By the way, I want to complain about the original poster’s English proficiency, code format and naming style! ! ! ! !

In other words, the second parameter of the removeListener event is undefined.

By the way, the code has been unified in style and the naming has been corrected to make it consistent with http://nodejs.org/api/events.html.

var emitter = new (require('events').EventEmitter);

emitter.on("newListener", function(event, listener) {
    // listener({event: event});
});
emitter.on("aaa", function(event) {
    console.log("Fire on register: " + value.event);
});// Fire on register: aaa

emitter.on("removeListener", function(event, listener) {
    console.log(event);
    listener({event: event});
});
function onddd(event) {
    console.log("Fire on remove: " + event.event);
}
emitter.on("ddd", onddd);
// emitter.removeListener("ddd", onddd);
emitter.removeAllListeners("ddd");

This code works fine because

emitter.on("newListener", function(event, listener) {
    // listener({event: event});
});
The function calls in

are commented out.

Because

emitter.on("removeListener", function(event, listener) {
    console.log(event);
    listener({event: event});
});

itself will also cause newListener to be called.

So, change the code to the following:

var emitter = new (require('events').EventEmitter);

emitter.on("removeListener", function onRemoveListener(event, listener) {
    listener({event: event});
});

emitter.on("ddd", function onDdd(event) {
    console.log("Fire on remove: " + event.event);
});

emitter.on("newListener", function onNewListener(event, listener) {
    listener({event: event});
});
emitter.on("aaa", function onAaa(event) {
    console.log("Fire on register: " + event.event);
});

emitter.removeAllListeners("ddd");

Actually, on and addListener are the same, so changing addListener to on will not escape newListener.

The poster who learns programming but doesn’t read the official documentation is worse than us novices who only read the official documentation but don’t learn programming. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template