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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:12:56+00:00 2026-06-12T10:12:56+00:00

Today I ran into a very odd problem using Javascript’s prototype and the this

  • 0

Today I ran into a very odd problem using Javascript’s prototype and the this reference.

Basically, I have two objects. One object makes use of a div element and makes is possible to register an onclick event on the div. The other object makes use of this functionality and registers the onclick event using the first object.

Here is the code:

My first object, which takes care of the div:

DivObject = function() {};
DivObject.prototype.setClickListener = function(clickListener){
    document.getElementById("myDiv").onclick = function(e){
        clickListener(e);
    };
};

My second object uses this functionality:

MainObject = function(){
    this.myString = "Teststring";
    this.divObj = new DivObject();
    this.divObj.setClickListener(this.handleClick);
};

MainObject.prototype.handleClick = function(e){
    // do something with e
};

The problem is, that inside MainObject.prototype.handleClick, the this reference refers to the window object, not to the MainObject. So, console.log(this) inside this function logs [Object window].

You can see this behaviour on jsfiddle.

As a workaround, I use the setClickListener function as follows:

var thisRef = this;
this.divObj.setClickListener(function(e){
    thisRef.handleClick(e);
});

Now, the this reference in my handleClick function refers to my MainObject (as I want it to be).

See this workaround on jsfiddle.

My questions are:

Why does the this reference one time refer to the window object and one time to the this reference of my object? Is it overridden somewhere? I always thought using this in my object I can be sure that it is really the this reference of my object? Is the workaround I am using now the way how this problem should be solved or are there any other, better way to handle this case?

  • 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-12T10:12:57+00:00Added an answer on June 12, 2026 at 10:12 am

    Your questions:

    Why does the this reference one time refer to the window object and one time to the this reference of my object?

    Other than using bind, the value of a function’s this is set by how the function is called. When you do:

    this.divObj.setClickListener(this.handleClick);
    

    you are assigning a function reference, so the function is called later without any qualification (i.e. it’s called as just handleClick rather than this.handleClick). On entering the function, because its this isn’t set by the call, it will default to the global (window) object, or in strict mode remain undefined.

    Is it overridden somewhere?

    No, the value of this is set on entering an execution context. You can’t overwrite it or directly assign to it, you can only set it in the call (e.g. as a method of an object, using new, apply, call) or using bind (also, arrow functions adopt the this of their enclosing lexical execution context).

    I always thought using this in my object I can be sure that it is really the this reference of my object?

    At the point you make the assignment, this is what you expect. But you are assigning a reference to a funciotn, not calling the function, so its this isn’t set at that moment but later when it’s called.

    Is the workaround I am using now the way how this problem should be solved or are there any other, better way to handle this case?

    Your work around is fine (and a common fix), it creates a closure so may have minor memory consequences but nothing serious. For very old versions of IE it would create a memory leak due to the circular reference involving a DOM object, but that’s fixed.

    The bind solution is probably better from a clarity and perhaps maintenance viewpoint. Remember to include a "monkey patch" for browsers that don’t have built–in support for bind.

    Please post code on SO, there is no guarantee that code posted elsewhere will continue to be accessible. The work around code:

    MainObject = function(){
        this.myString = "Teststring";
        this.divObj = new DivObject();
        var thisRef = this;
        this.divObj.setClickListener(function(e){
            thisRef.handleClick(e);
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I ran into the following problem with NUnit. I have a class, that
I ran into this situation today. I have an object which I'm testing for
I ran into a problem today at work wherein I have a BindingGroup that
I ran into a problem using java objects in jython today because jython is
Today I ran into a very difficult TDD problem. I need to interact with
I ran into a very weird problem today. Long story short, my function returns
I ran into a problem today when trying to set a field using FieldInfo.SetValue()
I ran into this today and have no idea why the C# compiler isn't
today I ran into this problem which really bugs me, as almost the code
I'm quite new to multithreading and today I ran into this strange problem. I

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.