In Node.js, Event is a very core module, and most core modules basically use or inherit from this module. The Event module is very similar to the EventTarget interface we studied earlier in Javascript. We can do more than simply use this module but also make extensions based on this module, you think of JS **onclick, onMousemove **
No wordy, straight up the code!
Because the event module is a relatively abstract thing, it is not suitable to describe it in a large language, so I decided to go directly to the code, first implement the function to see how it looks. Let’s experience the functions of this module.
Extend custom classes
On the front end we can listen for events on an element, for example
button.addEventListener('click'()=>{
alert('I got clicked.');
},false);
Copy the code
This is essentially adding event listeners to the DOM object on the page. In Node.js you cannot add event listeners to an object directly because our custom classes or objects do not implement or inherit the Events module.
To use the Events module, you need to import it first.
const event = require('events');
Copy the code
Next comes the implementation of the concrete code
// The official website case adaptation
The alarm class inherits the event module
class Clock extends event{
constructor(name){
// Call the parent constructor
super(a);this.name = name;
// Call its own alarm method
this.ring();
}
ring(){
// The alarm goes off every three seconds
setInterval(() = >{
// Trigger the ring event
this.emit('ring');
},3000); }}let clock1 = new Clock('pig');
// Let the clock1 object listen for the ring event
clock1.on('ring'.function(){
console.log(`The ${this.name}The alarm clock is waking you up! `);
});
Copy the code
The above code is designed to extend our custom class. In the above code we used two methods from the Event module: on() and emit(). These methods are used to register events and trigger events, respectively. There are far more useful methods in the Event module than these two. Here are some of the more common ones.
on()
: Registers events, adding the name of the event being listened to to the end of the listener array.addListener()
: same as above.off()
: Removes the name of the event being listened to from the listener array.removeListener()
: same as above.eventNames()
: returns an array of event names for registered listeners. The values in the array are strings or symbols.
Tips:
Because the concept of Events is relatively abstract, most of us developers do not like to use Events module to implement monitoring. Most of the time, we may prefer to complete the monitoring of a function or method in the way of callback function.
Therefore, our learning of Events module is not only to add Events to the custom class in the future development, but also to have a certain understanding of the overall structure of Knowledge points of Node.js and understand the ins and outs of knowledge points.
After learning Events, we can understand why many core modules learned later have on() and off() methods, because they inherit Events module.