Here i cannot understand what is the basic difference between these two methods.
在这里我无法理解这两种方法的基本区别。
var events = require('events');
var eventEmitter = new events.EventEmitter();
var listner1 = function listner1() {
console.log('listner1 executed.');
}
var listner2 = function listner2() {
console.log('listner2 executed.');
}
eventEmitter.addListener('connection', listner1);
eventEmitter.on('connection', listner2);
eventEmitter.emit('connection');
3 个解决方案
#1
54
.on()
is exactly the same as .addListener()
in the EventEmitter object.
on()与event发射器对象中的. addlistener()完全相同。
Straight from the EventEmitter source code:
直接从EventEmitter源代码:
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'".
通过GitHub存储库进行侦察,这里有一个来自2010年7月3日的检入,其中包含了注释:“Experimental:‘on’作为‘addListener’的别名”。
Update in 2017: The documentation for EventEmitter.prototype.addListener()
now says this:
2017年更新:EventEmitter.prototype.addListener()的文档如下:
Alias for
emitter.on(eventName, listener)
.发射器的别名。(eventName侦听器)。
#2
1
Yes you can use "removeListener" with with a listener created with "on". Try it.
是的,你可以使用“removeListener”和一个用“on”创建的监听器。试一试。
var events = require('events');
var eventEmitter = new events.EventEmitter();
// listener #1
var listner1 = function listner1() {
console.log('listner1 executed.');
}
// listener #2
var listner2 = function listner2() {
console.log('listner2 executed.');
}
// Bind the connection event with the listner1 function
eventEmitter.addListener('connection', listner1);
// Bind the connection event with the listner2 function
eventEmitter.on('connection', listner2);
var eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
// Fire the connection event
eventEmitter.emit('connection');
// Remove the binding of listner1 function
eventEmitter.removeListener('connection', listner2);
console.log("Listner2 will not listen now.");
// Fire the connection event
eventEmitter.emit('connection');
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
console.log("Program Ended.");
#3
0
Their functionalities are exactly the same, however, they can be used in different ways to make your code efficient. Lets assume you created a server and you create a listener, by using ".addListener(event, listener)", for every user that connects to your server. Now as soon as a user is disconnected, you can remove that listener by using the command "removeListener", but you cannot remove the ".on(event, listener)" command. So, you can use these two commands for different situations.
它们的功能是完全相同的,但是它们可以用不同的方法来提高代码的效率。假设您创建了一个服务器,并通过“”创建了一个侦听器。addListener(事件侦听器),用于连接到服务器的每个用户。现在,一旦用户断开连接,就可以使用“removeListener”命令删除该侦听器,但不能删除“”。(事件侦听器)”命令。因此,您可以在不同的情况下使用这两个命令。
#1
54
.on()
is exactly the same as .addListener()
in the EventEmitter object.
on()与event发射器对象中的. addlistener()完全相同。
Straight from the EventEmitter source code:
直接从EventEmitter源代码:
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'".
通过GitHub存储库进行侦察,这里有一个来自2010年7月3日的检入,其中包含了注释:“Experimental:‘on’作为‘addListener’的别名”。
Update in 2017: The documentation for EventEmitter.prototype.addListener()
now says this:
2017年更新:EventEmitter.prototype.addListener()的文档如下:
Alias for
emitter.on(eventName, listener)
.发射器的别名。(eventName侦听器)。
#2
1
Yes you can use "removeListener" with with a listener created with "on". Try it.
是的,你可以使用“removeListener”和一个用“on”创建的监听器。试一试。
var events = require('events');
var eventEmitter = new events.EventEmitter();
// listener #1
var listner1 = function listner1() {
console.log('listner1 executed.');
}
// listener #2
var listner2 = function listner2() {
console.log('listner2 executed.');
}
// Bind the connection event with the listner1 function
eventEmitter.addListener('connection', listner1);
// Bind the connection event with the listner2 function
eventEmitter.on('connection', listner2);
var eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
// Fire the connection event
eventEmitter.emit('connection');
// Remove the binding of listner1 function
eventEmitter.removeListener('connection', listner2);
console.log("Listner2 will not listen now.");
// Fire the connection event
eventEmitter.emit('connection');
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
console.log("Program Ended.");
#3
0
Their functionalities are exactly the same, however, they can be used in different ways to make your code efficient. Lets assume you created a server and you create a listener, by using ".addListener(event, listener)", for every user that connects to your server. Now as soon as a user is disconnected, you can remove that listener by using the command "removeListener", but you cannot remove the ".on(event, listener)" command. So, you can use these two commands for different situations.
它们的功能是完全相同的,但是它们可以用不同的方法来提高代码的效率。假设您创建了一个服务器,并通过“”创建了一个侦听器。addListener(事件侦听器),用于连接到服务器的每个用户。现在,一旦用户断开连接,就可以使用“removeListener”命令删除该侦听器,但不能删除“”。(事件侦听器)”命令。因此,您可以在不同的情况下使用这两个命令。