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 8052027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:26:57+00:00 2026-06-05T07:26:57+00:00

I have an event/listener manager that has this function: var addListener = function(event, listener)

  • 0

I have an event/listener manager that has this function:

  var addListener = function(event, listener) {
    myListeners[event].push(listener); //assume this code works
  }

But now I need to change it so that it looks like this:

  var addListener = function(event, listener, fireFirst) {
    if(fireFirst) {
      myListenersToFireFirst[event].push(listener);
    } else {
      myListenersToFireSecond[event].push(listener);
    }
  }

This is so that when the fireEvent function is called, it will fire the listeners in the myListenersToFireFirst array first, then the listeners in the second array.

So it will look something like this:

  var fireEvent = function(event) {
    var firstListeners = myListenersToFireFirst[event];
    //for each listener in firstListeners, call `apply` on it

    var secondListeners = myListenersToFireSecond[event];
    //for each listener in secondListeners, call `apply` on it
  }

Is this the best way to accomplish this in JavaScript? Is there a more elegant way of achieving this priority list of listener-event firing?

  • 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-06-05T07:26:57+00:00Added an answer on June 5, 2026 at 7:26 am

    maybe its a better way then mine.. but that is a specific way, i mean you have to insert new handlers in the block. This is a more generic tool, however it seems to have application on your case.

    i suggest this:

    //makes a closured function that calls this function after the other
    Function.prototype.prefix=function(o) {
        var that=this;
        return function(){
            that.apply(this,arguments);
            return o.apply(this,arguments);
        };
    }
    //prefix=reversed sufix
    Function.prototype.sufix=function(o) {return o.prefix(this);}
    

    with this code you can append/prepend functions to each other to form a kind of chain, its usefull to add another listener or to track a functions usage, or even do adapt code with minimal impact.

    some usage

    function audit() {console.log(arguments.callee.caller,arguments.callee.name,arguments);}
    function a() {alert(arguments);}
    a=audit.prefix(a);
    
    
    //a user function
    function f() {alert(arguments);}
    f("test");//just do the alert as defined
     f=audit.prefix(a);
    f("test");//now arguments will be on console too
    
    //a builtin function
    //this audit example only write to the console the arguments being passed to it
    function audit() {console.log(arguments.callee,arguments);}
    //auditing alert function (not too usefull, but shows it works even for
    alert=audit.prefix(alert);//alert is now prefixed with audit
    
    //an event handler
    document.body.onclick=function() {alert("clicked");};
    document.body.onclick=audit.prefix(document.body.onclick);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have more than one Event Listener that calls a specific function, and I
I have event manager process that dispatches events to subscribers (e.g. http_session_created, http_sesssion_destroyed). If
I have manager classes that take an activity as a listener. I use the
I have a JFrame with a CardLayout set as its layout manager. This has
Is it possible to have keyboard event listener canvas.addEventListener('onkeydown', ev_keydown, false); like we have
I have the following event listener in SecurityConfig security { active = true useSecurityEventListener
I have implemented Shake Event listener in my application and onShake do some logic.
Currently, I have a JList listen to list selection listener. private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt)
Input rz value In the head I have: Event.observe(window, 'load', function() { $$(input).each(function(field){ Event.observe(field,
I have used this code in my AS3 document class to remove all objects

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.