What is the difference between addListener(event, listener) and on(event, listener) method in node.js?

前端 未结 4 590
余生分开走
余生分开走 2020-12-02 19:41

Here i cannot understand what is the basic difference between these two methods.

var events = require(\'events\');
var eventEmitter = new events.EventEmitter         


        
4条回答
  •  日久生厌
    2020-12-02 20:34

    .on() is exactly the same as .addListener() in the EventEmitter object.

    Straight from the EventEmitter source code:

    EventEmitter.prototype.on = EventEmitter.prototype.addListener;
    

    Sleuthing through the GitHub repository, there is this checkin from Jul 3, 2010 that contains the comment: "Experimental: 'on' as alias to 'addListener'".


    Update in 2017: The documentation for EventEmitter.prototype.addListener() now says this:

    Alias for emitter.on(eventName, listener).

提交回复
热议问题