Starting from today, we will introduce some important APIs of node. First, we will introduce events to you. It is a very important API in node and also the basis for implementing other APIs. A good understanding of it will help you use other APIs.
If you have developed browser applications using JavaScript, you must be familiar with the events. However, events in the browser come from Dom rather than JavaScript. User-driven events in Dom interact with users through a series of Tree Elements (html/XML) interfaces. When users interact with Dom, corresponding events are generated.
1. eventemitter
Node does not have the DOM object, so it creates its own eventemitter class to implement basic event functions. Other events in node are implemented through this interface class. The two most important eventemitter methods are on and emit for other classes. On implements the listening function for events. It has two parameters: the first is the listening event, and the second is the corresponding callback function. Let's look at an example:
server.on('event', function(a, b, c) { //do things});
Eventemitter is an interface, so we need to create a class that inherits from it and use the newly created class to implement the event, instead of directly using it. See the following example:
var utils = require('utils'),EventEmitter = require('events').EventEmitter;var Server = function() { console.log('init');};utils.inherits(Server, EventEmitter);
var s = new Server();s.on('abc', function() { console.log('abc');});
Through the above steps, we have created a server class and made it inherit from eventemitter, so that we can use the corresponding methods in eventemitter. For example, we used on to listen to an event called 'abc. How can we trigger this event? Use the following statement.
s.emit('abc');
It should be emphasized that the events are instance-level and there is no global event. The instance objects of different servers cannot share their events. For example, the above S object cannot share the Z event of the following object,
var z = new Server();.
2. Callback
When using events, a very important thing is to use callback functions. Let's take a look at the callback function mechanism in node.
When using the emit method, in addition to the event name parameter, we can also add any other parameters, such:
s.emit('abc', a, b, c);
Other parameters are passed to the callback function. When we use the emit method, the following code accesses each listener event.
if (arguments.length <= 3) { // fast case handler.call(this, arguments[1], arguments[2]);} else { // slower var args = Array.prototype.slice.call(arguments, 1); handler.apply(this, args);}
Depending on the length of the parameter, the code will choose whether to use call or apply. Note that the first parameter this means that the context of the access event listener is the context of eventemitter, rather than the original context of the Code. With node repl, you will see what happened with eventemitter. Run the Start menu, select all programs, and find node. JS (x86)], click node. JS], enter the following content line by line (SHIFT + press ENTER for line feed), or paste and copy the content as follows:
var EventEmitter = require('events').EventEmitter,util = require('util');var Server = function() {};util.inherits(Server, EventEmitter);Server.prototype.outputThis= function(output) { console.log(this); console.log(output);};
Server.prototype.emitOutput = function(input) { this.emit('output', input);};Server.prototype.callEmitOutput = function() { this.emitOutput('innerEmitOutput');};
var s = new Server();s.on('output', s.outputThis);s.emitOutput('outerEmitOutput');s.callEmitOutput();s.emit('output', 'Direct');
Result:
Note the Blue Line, which is the output content of console. Log (this). It can be seen that this points to the eventemitter object no matter in that case.