is event emitter baked in to all objects?

Posted on
  • In node/browser if I need to incorporate an event emitter I either use the native events module or third party. Typically I'd create a new class by extending the event emitter. Like so. https://nodejs.org/api/events.html

    import EventEmitter from 'events'
    class SomeClass extends EventEmitter {
      constructor() {
      }
    }
    

    but it seems that all (plain?) objects in Espruino have an event emitter baked in by default. https://www.espruino.com/Reference#l_Object_emit

    So when I make a new class or make an object like so I get an event emitter as well?

    Is the actual event emitter module available? If so where is that in the api docs

    const obj = {}
    
    class SomeClass {
      constructor() {
      }
    }
    
    const cls = new SomeClass()
    obj.on('test',  val => { console.log(val) })
    cls.on('test',  val => { console.log(val) })
    cls.emit('test','class')
    obj.emit('test','object')
    
    
  • Yes, that's right. There's no explicit EventEmitter class. At the moment it is in all objects by default - initially the performance cost of adding it to every object was less than having two different kinds of object. It's something I did plan on changing at some point but it's been low priority.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

is event emitter baked in to all objects?

Posted by Avatar for dgk @dgk

Actions