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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:15:32+00:00 2026-05-27T16:15:32+00:00

This question relates specifically to JavaScript, but I imagine would apply to any language.

  • 0

This question relates specifically to JavaScript, but I imagine would apply to any language.

I am new to OOP and I’m just looking to see if there is a best practice/better way of doing this. I have an object that needs to subscribe to some events. I put the event subscriptions inside the objects constructor, like so:

function Obj() {
    //instance variables

    outsideEvent.addHandler(function(response) {
        //do stuff
    });
}

The problem is that now I feel like the object is coupled to the publisher; I would like if I could somehow subscribe outside of the object itself. Unfortunately I don’t know when or where a new instance of this object might be initialized. I thought about making an “attachEvents” member function, but that function would have to be called whenever a new instance is made or called in the constructor, but maybe that is a best practice? It would at least make it easier to unit test.

I’m just looking for some advice; anything is appreciated!

  • 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-27T16:15:32+00:00Added an answer on May 27, 2026 at 4:15 pm
    function Constructor(eventEmitter) {
        /* code */
        this.listenTo(eventEmitter);
    }
    
    Constructor.prototype.listenTo = listenTo;
    
    function listenTo(eventEmitter) {
        /* bind events */
    }
    

    Alternatively rather then passing an eventEmitter into your constructor, your constructor can inherit from eventemitter and be one itself.

    This means it can listen on itself. You would then publish events on it rather then some external eventEmitter.

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

Sidebar

Related Questions

This question specifically relates to my experience with PHP, but there's no reason why
This question relates to Java collections - specifically Hashtable and Vector - but may
This is kind of a two part question. But it both relates to the
I know this is not a real programming question. But, it relates to programming
(Related to this question .) I'm looking for a wysiwyg editor, but which outputs
This question is specifically related to overriding the equals() method for objects with a
This question relates to an ASP.NET website, originally developed in VS 2005 and now
This question relates to this question which I asked earlier this week. The answer
This question relates to those parts of the KenKen Latin Square puzzles which ask
Background: This question relates to versions of Delphi below 2009 (ie without Unicode support

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.