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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:11:11+00:00 2026-05-25T14:11:11+00:00

I have a problem with eventlisteners in javascript that is probably due to my

  • 0

I have a problem with eventlisteners in javascript that is probably due to my incomplete control of the language.

I wish to register event listeners using addEventListener() / attachEvent(); I also want to be able to remove the listeners later on, so I can’t register anonymous functions.

Say I now want to register a method of an object as an event listener. First I’d expected addEventListener(event, node, object.method) to work. However, after reading up on events I now understand that the this keyword will refer to the event target when the event listener is called, not my object.

To work around this, I have created a new method, methodCallback of the object, like so:

object.methodCallback = function () {
    self.method(); 
}

the self variable is set to copy this in the object constructor. I then register object.methodCallback as an eventlistener.

I feel this must be the wrong way to do it. But what is the right way?

I apologize if this question is common, but I haven’t been able to find an answer on SO so far

  • 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-25T14:11:12+00:00Added an answer on May 25, 2026 at 2:11 pm

    It’s common for people to use self in such a way. I would say use ‘self’ if you are within a class and ‘that’ if not.

    You need to define self / that one level up in the scope chain.

    var self = this;
    object.methodCallback = function () {
        self.method(); 
    }
    

    And yep, that’s the way it rolls. Unfortunately.

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

Sidebar

Related Questions

I have a few JavaScript functions that behave as event listeners for an <object/>
I have problem in some JavaScript that I am writing where the Switch statement
I have a javascript problem that I believe is related to closures in loops
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run
somewhat simple problem(to explain) this time: i have an array of markers that i
Possible Duplicate: flash AS3 how do I remove stage event listeners Say I have
In a C# winforms program, I have an event that fires when someone attempts
If i have something like this, where i can control the auto-scrolling using boolean
I have a Google Map, and I am adding event listeners to different things.

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.