search

Home  >  Q&A  >  body text

node.js - nodejs removeListener 無法得到 listener

event.on("newListener", function (EventName, callback) {
    callback({on: EventName});
});
event.addListener("aaa", function (value) {
    console.log("我是注册时自动触发的" + value.on + "事件!");
});// 正常输出:我是注册时自动触发的 aaa 事件!

event.on("removeListener", function (EventName, callback) {
    callback({EventName: EventName});
});
var callbackName = function (value) {
    console.log("我是删除事件时自动触发的" + value.EventName + "事件!");
}
event.on("ddd", callbackName);
event.removeListener("ddd", callbackName);
// 报错!event.js:76 callback({EventName: EventName});
PHP中文网PHP中文网2863 days ago1074

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 11:33:13

    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. . .

    reply
    0
  • Cancelreply