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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:48:10+00:00 2026-06-11T22:48:10+00:00

Heres a bit of cleaned up code from a picture gallery im writing to

  • 0

Heres a bit of cleaned up code from a picture gallery im writing to learn js.
I create the gallery as an object but at some point i lose track of what “this” points to.
It doesnt make sense to me what happens at the bottom (look at the comments).
Can someone please explain?

function Gallery(parentID)
{
        // ...
    this.showDiv = document.createElement("div");
        // ...
    this.show = function ()
        {
        document.body.appendChild(this.showDiv); //will be given css absolute position to "pop up"
        this.showDiv.innerHTML = '<img class="displayImage" src="' + this.picList[this.showIndex] + '">'; //fill with image
        this.showDiv.focus();
        this.showDiv.onclick = this.hide;
        }   

    this.hide = function ()
        {
        alert(this.innerHTML); // <= WHY DOES THIS SHOW THE INNERHTML CONTENTS OF this.showDiv??????
            //alert(this.showDiv.innerHTML); // <= shouldnt this be correct?
        this.parentNode.removeChild(this); //doesnt work
        }
}

Let me know if i cleaned up some code that might have affected the results and ill fill it in.

Thanks.

  • 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-11T22:48:11+00:00Added an answer on June 11, 2026 at 10:48 pm
        this.showDiv.onclick = this.hide;
    

    This line is the problem. It doesn’t work like in Python; this.hide is not a bound method. It’s just the hide function, not bound to any particular this.

    One way to fix it is:

        this.showDiv.onclick = this.hide.bind(this);
    

    But the .bind() method of functions is a rather new standard; it isn’t in all old browsers. So you might want to roll your own instead:

    function bindMethod(object, func) {
        return function () { return func.apply(object, arguments); };
    }
    
    this.showDiv.onclick = bindMethod(this, this.hide);
    

    (Generally this is kind of strangely behaved in JavaScript; the main thing to remember is that this always refers to the innermost function’s this, which could be whatever the caller wants it to be. Sometimes you see JS code do things like var self = this; to give a particular function’s this a name that can be used in nested functions; in Python, of course, that is just sort of the way things work automatically.)

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

Sidebar

Related Questions

Here's the relevant bit of the source code: class Dice { String name ;
Bit confused here, I have an on-demand instance but do I get charged even
I might be being a bit thicky here but please answer me this. Consider
Have a bit of confusion here. I added a few objects from a class
Here's a bit of a twist on the old which do I learn first
i found this code online and im trying to compile it, but gcc keeps
I wanted to my code a bit cleaner so I wanted to put a
I'm finding myself doing more C/C++ code against Win32 lately, and coming from a
I've asked a similar question before, but I've done some more research and this
This is probably a bit subjective, but I'm in fact looking for answers that

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.