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

  • Home
  • SEARCH
  • 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 7635005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:19:51+00:00 2026-05-31T07:19:51+00:00

I am trying to create a custom javascript EventManager class. I’ve adopted the format

  • 0

I am trying to create a custom javascript EventManager class. I’ve adopted the format Grant Skinner uses in his easel.js framework for creating the class and need to stick with it. I’m really just lost at this point – I think that – at least in a conceptual sense – I have the right idea here and that it’s mostly scope issues that evade me.

I’m hoping someone here can help me take this to the point where addListener and dispatchEvent are functional.

[code]

(function(window) {
    var EventManager = function() {
        this.initialize();
    }
    var p = EventManager.prototype;

    // place properties here



    // Constructor
    p.initialize = function() {
        p.listeners = new Array();
    }

    // public methods


    p.addListener = function(fn, event) {
        console.log("p.addListener Hit");
        console.log("event: " + event);
        console.log("handler function: " + fn);
        if(!this.listeners[event]) {
            this.listeners[event] = [];
        }

        if(fn instanceof Function) {
            this.listeners[event].push(fn);
        }
        return this;
    }

    p.dispatchEvent = function(event, params) {
        console.log("Dispatch Event");
        // loop through listeners array
        for(var index = 0; index < listeners[ev].length; index++) {
            // execute matching 'event' - loop through all indices and
            // when ev is found, execute
            listeners[event][index].apply(window, params);
        }
    }

    p.removeListener = function(event, fn) {
        // split array 1 item after our listener
        // shorten to remove it
        // join the two arrays back together

    }
    window.EventManager = EventManager;

}(window));


[/code]
[code]
    <script>

    eventManager = new EventManager();

    var FooTest = function() {
        this.fire = function() {
           //alert("fire");
        }

            this.fire();
     };

    function onFire() {
       // console.log("FIRED!");
    }

    var o = new FooTest();
   eventManager.addListener.call("fire", onFire );
   // eventManager.dispatchEvent.call(o,'fire' );

    </script>
[/code]
  • 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-31T07:19:52+00:00Added an answer on May 31, 2026 at 7:19 am

    Here’s a working example of fixed code: http://jsfiddle.net/JxYca/3/

    For the most part your code was working, just a few small problems here and there. IFFE is Immediately-Invoked Function Expression (IIFE). This is what you were doing with the whole function(window) {}(window). However in this case it’s absolutely unnecessary and just pollutes the code. There’s no such thing as hashtable in javascript, however, you can just use an object instead of it. The names of the properties become a key and their value is now value of the hashtable.

    An additional and sort of unrelated though for you. This way of doing events in nice, but if you have, say, 3 handlers attached to an event, and second one fails with JavaScript exception, third one will never get executed. You might want to take a quick look at how prototype.js does events here: https://github.com/sstephenson/prototype/blob/master/src/prototype/dom/event.js Their events are non-blocking.

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

Sidebar

Related Questions

I'm trying to create a custom javascript class that inherits from google.maps.Map (V3 API).
Trying to create a custom javascript slider, the idea is it cycles through 4
I've been trying to create my own custom scroll bar implementation in JavaScript. It
Im trying to create a custom version of the RequiredAttribute to replace the built
I'm trying to create a custom UIButton that should look like a UIButtonTypeRoundedRect. In
I'm trying to create a custom component with a collection property. However if I
I'm trying to create a custom button where the foreColor is always crimson and
I'm trying to create a custom MembershipProvider and I was wondering how I would
I am trying to create a custom property in a extended control, so that
I am trying to create a custom wpf control, I'm wondering how I can

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.