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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:14:58+00:00 2026-06-18T07:14:58+00:00

Being a long time C++/C# developer, I find myself moving a lot of of

  • 0

Being a long time C++/C# developer, I find myself moving a lot of of my JS code into “classes” to group functions and data together. As those classes handle event though, I’m finding myself having to write “stub” handlers that serve only to route the calls into a class method to provide the proper this context. So I’m doing things like this:

var Manager = {
    foo: 'x',
    bar: 1,

    onClickStub: function(evt) {
        // 'this' refers to HTMLElement event source
        Manager.onClick(evt);
    },

    onClick: function(evt) {
        // 'this' now refers to Manager.
        // real work goes here.
    }
}

Is this the normal way of doing things or is there a better way to structure my event handlers while keeping my class organization?

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

    As Joseph Silber said in the comments above, I think bind would be perfect in this case. If you need to support older browsers, you can always add a shim to Function.prototype.bind (see an example implementation in the MDN docs). Then your code could just be:

    var Manager = {
        var foo: 'x',
        var bar: 1,
    
        // no more stub!
    
        onClick: function(evt) {
            // 'this' will refer to Manager.
            // real work goes here.
        }
    }
    
    // And when you bind the event handler:
    var el = document.getElementById('something');
    el.addEventListener('click', Manager.onClick.bind(Manager));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being a long-time POSIX/Linux/BSD C developer and now trying to do a few things
I just started programming for Android after being a long time Java developer and
I've been experimenting with Adobe Flex recently. Being a long-time server-side web app developer,
long time ASP.Net interface developer being asked to learn WCF, looking for some education
Hai guys, I had this doubt for a long time now. Now being a
Newly registered user here after being a long time lurker! I have an Apache
I'm being doubtful for a long time about how to design parametric tables... Do
I have being researching regarding this question for a long time but I was
I have recently started learning nodeJS. Being a long-time coffeescript lover I naturally decided
I have a somewhat-long-running stored procedure being called from a PB application. I want

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.