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

  • Home
  • SEARCH
  • 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 67839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:19:22+00:00 2026-05-10T19:19:22+00:00

I’m having some trouble with plain old JavaScript (no frameworks) in referencing my object

  • 0

I’m having some trouble with plain old JavaScript (no frameworks) in referencing my object in a callback function.

function foo(id) {     this.dom = document.getElementById(id);     this.bar = 5;     var self = this;     this.dom.addEventListener('click', self.onclick, false); }  foo.prototype = {     onclick : function() {         this.bar = 7;     } }; 

Now when I create a new object (after the DOM has loaded, with a span#test)

var x = new foo('test'); 

The ‘this’ inside the onclick function points to the span#test and not the foo object.

How do I get a reference to my foo object inside the onclick function?

  • 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. 2026-05-10T19:19:23+00:00Added an answer on May 10, 2026 at 7:19 pm

    (extracted some explanation that was hidden in comments in other answer)

    The problem lies in the following line:

    this.dom.addEventListener('click', self.onclick, false); 

    Here, you pass a function object to be used as callback. When the event trigger, the function is called but now it has no association with any object (this).

    The problem can be solved by wrapping the function (with it’s object reference) in a closure as follows:

    this.dom.addEventListener(   'click',   function(event) {self.onclick(event)},   false); 

    Since the variable self was assigned this when the closure was created, the closure function will remember the value of the self variable when it’s called at a later time.

    An alternative way to solve this is to make an utility function (and avoid using variables for binding this):

    function bind(scope, fn) {     return function () {         fn.apply(scope, arguments);     }; } 

    The updated code would then look like:

    this.dom.addEventListener('click', bind(this, this.onclick), false); 

    Function.prototype.bind is part of ECMAScript 5 and provides the same functionality. So you can do:

    this.dom.addEventListener('click', this.onclick.bind(this), false); 

    For browsers which do not support ES5 yet, MDN provides the following shim:

    if (!Function.prototype.bind) {     Function.prototype.bind = function (oThis) {       if (typeof this !== 'function') {         // closest thing possible to the ECMAScript 5 internal IsCallable function         throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');       }        var aArgs = Array.prototype.slice.call(arguments, 1),            fToBind = this,            fNOP = function () {},           fBound = function () {             return fToBind.apply(this instanceof fNOP                                    ? this                                    : oThis || window,                                  aArgs.concat(Array.prototype.slice.call(arguments)));           };        fNOP.prototype = this.prototype;       fBound.prototype = new fNOP();        return fBound;     };   }  
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 155k
  • Answers 155k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are trying to set the height of the node… May 12, 2026 at 10:41 am
  • Editorial Team
    Editorial Team added an answer You would need to do something like this: <WebMethod()> Public… May 12, 2026 at 10:41 am
  • Editorial Team
    Editorial Team added an answer As I understand it, you are effectively asking for ways… May 12, 2026 at 10:41 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.