Recent Changes - Search:

edit SideBar

Events

This module, which is borrowed from Node.js, contains a single class definition, EventEmitter. A typical usage pattern is to inherit it. For example:

  var events = require('events');
  var util = require('util');

  function MyClass() {
    // Invoke the event emitter constructor.
    events.EventEmitter.call(this);
    util.log('instantiated');
  }
  util.inherits(MyClass, events.EventEmitter);

  var instance = new MyClass();

  instance.on('ping', function() { util.log('ping'); });
  instance.emit('ping');

This code defines a class that subclasses EventEmitter. As a consequence, the class inherits the functions on() and emit(). The first of these functions defines what action to take when a named event is emitted. The last two lines of the code above define the reaction to an event named 'ping' and then emit the event. The result of executing this code is, for example:

  2 May 14:16:33 - instantiated
  2 May 14:16:33 - ping

The events module is defined more fully at https://nodejs.org/api/events.html. It include the following:

  • Class: events.EventEmitter
    • emitter.addListener(event, listener)
    • emitter.on(event, listener)
    • emitter.once(event, listener)
    • emitter.removeListener(event, listener)
    • emitter.removeAllListeners([event])
    • emitter.setMaxListeners(n)
    • EventEmitter.defaultMaxListeners
    • emitter.listeners(event)
    • emitter.emit(event[, arg1][, arg2][, ...])
    • Class Method: EventEmitter.listenerCount(emitter, event)
    • Event: 'newListener'
    • Event: 'removeListener'

Remote Resources


Back to Built-In JavaScript Modules

Edit - History - Print - Recent Changes - Search
Page last modified on November 20, 2017, at 08:30 AM