Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:51:40+00:00 2026-05-27T10:51:40+00:00

I write lots of little libraries and modules and usually these libraries and modules

  • 0

I write lots of little libraries and modules and usually these libraries and modules have have some events associated with them. Until now I’ve been writing these like (shortened down a lot):

Library.prototype.on = function(event, callback){
  self = this;
  self.events[event] = callback;
}

then a user would do something such as:

Library.on('foo',function(){
  console.log('bar');
});

Is there a better more performant way to do this though or is this a standard way of implementing this? I want a simple API that i can drop into any JS project to support this behavior.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:51:41+00:00Added an answer on May 27, 2026 at 10:51 am
    var EventEmitter = {
        constructor: function _constructor() {
            this._events = [];
            return this;
        },
        on: function _on(ev, handler) {
            if (!this._events[ev]) {
                this._events[ev] = [];
            }
            this._events[ev].push(handler);
        },
        removeListener: function _removeListener(ev, handler) {
            if (!this._events[ev]) return;
            this._events[ev].splice(this._events[ev].indexOf(handler), 1);
        },
        removeAll: function _removeAll(ev) {
            delete this._events[ev];
        },
        emit: function _emit(ev, data) {
            if (!this._events[ev]) return;
            this._events[ev].forEach(invokeHandler);
    
            function invokeHandler(handler) {
                handler(data);
            }
        }
    };
    

    I have a small EventEmitter I use when I need quick and dirty custom events.

    The only difference between mine and your implementation is that mine allows multiple callbacks per event.

    For anything serious I use an event library like EventEmitter2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am generating lots of images in java and saving them through the ImageIO.write
I have a little sample application I was working on trying to get some
I have to write a program with 3 classes and lots of different methods.
Ok, I need a little help. I have these two PHP classes class menu
In the application I am writing, I need to write lots of base types,
I see lots of TDD practitioners following this cycle: 1) Write your test as
Write a program to determine whether a computer is big-endian or little-endian. bool endianness()
I habitually write code with lots of functions, I find it makes it clearer.
I've been stuck in a MsSql/MySql world now for a few years, and I've
One thing I have noticed on some sites is that they use one BIIIIIIIG

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.