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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:28:16+00:00 2026-06-13T07:28:16+00:00

I want to set up a observer/listener system: var listeners = []; function MyObj(){

  • 0

I want to set up a observer/listener system:

var listeners = [];

function MyObj(){
    this.myMethod = function(){
        // react to event
    }
}

var myObj = new MyObj();

function addListener(fn, obj){
    alliancesNotify[alliancesNotify.length] = {fn: fn, obj: obj}
}

addListener(myObj, myObj.myMethod); // Best way of doing it?

function notify(objArr){
    for (var i in objArr){
        objArr[i].obj.fn(); // will this work? 
    }
}

notify(listeners);

I was hoping for your thoughts on the two commented lines, (if it would actually work as expceted) and if there is perhaps a more straight forward way of notifying objects through calling their methods. (e.g. I’ve noticed some implementations pass the function name as a string instead of a reference to the function, instead of this way.)

Thanks

  • 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-13T07:28:17+00:00Added an answer on June 13, 2026 at 7:28 am

    Ok first of all there a multiple issues lets take them one by one:

    • objArr[i].obj.fn(); is not going to do what you expect. It’s going to look for and invoke the fn method, you were however looking forward to invoke myMethod from obj. To fix this you have multiple options. Either use apply, or call. So your code will look something like objArr[i].fn.call(objArr[i].obj). This is how good JS libraries do it like jQuery or Mootools or PrototypeJS.
    • Second issue, what if the callback function throws exception and has an error? In some browsers the loop will stop iterating and thus it will result in no further callbacks (functions) being invoked. Solutions can be multiple again, either use try catch block or do what’s my fav way of doing it. Use setTimeout so your code will look something like given below, Since JS is single threaded browser will queue the callback functions and even incase of errors, rest of them won’t be effected.
        function notify(objArr){
            for (var i in objArr){
                window.setTimeout(function(){ objArr[i].call(objArr[i].obj);}, 0);
            }
        }
    
    • It’s definitely not the optimum but close to optimum solutions around, so have a look on many libraries around (Google it) to improve it. I have myself written one such standalone PUB SUB library for event management check it out here
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set an observer for each of the following checkboxes: <div id=listr>
I want to set up an observer (using Prototype) which will be triggered when
I want an event notification system that should notify the doctor when the heartbeat
I want to trigger an email if a new system admin is granted. I
I want set interfaceOrientation in one UIView as LandscapeLeft and Portrait in other UIView.
i want set the visibility to itemized overlay in map view. if the zoom
I want set Listbox background to transparent but not working Is there any idea?
i want set class for repeater when doPostBack for those dataID i wan but
My question is : I have an EditText and I want set a margin
i have a problem, i want set text of a UILabel or UItextView or

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.