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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:35:59+00:00 2026-05-23T18:35:59+00:00

I had a class defined and was making only one instance of it. The

  • 0

I had a “class” defined and was making only one instance of it. The instance possessed a member function that would end up being passed around (it’s a mouse handler, but that’s not important). Since I would only ever make one instance of my “class”, I decided to rewrite it as a singleton by using an object literal.

So I have

var mySingleton = {
    theObjects : [];
}

mySingleton.mouseHandler = (function() {
    var that = this;
    return function (e) {
        for (var indx = 0; indx < that.theObjects.length; indx++) {
            // do something to that.theObjects[indx];
        }
    }
}());

mySingleton.addObject = function(newObj) {
    this.theObjects.push(newObj);
}

However, when I try to use this code (after adding a few objects), I keep getting an that.theObjects is undefined error. It’s referring to the line in the for loop.

  • 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-23T18:35:59+00:00Added an answer on May 23, 2026 at 6:35 pm

    Update for 2015 – Use Function.bind() to specify the value of this within the function. Then, instead of using that, you can use this.

    mySingleton.mouseHandler = function (e) {
        for (var indx = 0; indx < this.theObjects.length; indx++) {
            // do something to this.theObjects[indx];
        }
    }.bind(mySingleton);
    

    This doesn’t work if you want mouseHandler to have the context of the ‘moused’ element. For that, use my original answer below.

    If you need to support IE8 or (heaven forbid) earlier, you’ll need to use a polyfill.


    Since you are calling the function that creates mouseHandler immediately, it is run in the context of window, not mySingleton. So that refers to window. Instead of calling it immediately, just change it to a method so that it runs in the context of mySingleton:

    mySingleton.getMouseHandler = function() {
        var that = this;
        return function() { ... };
    };
    myElement.onclick = mySingleton.getMouseHandler();
    

    Of course, since you are already using a singleton, you can just reference it directly. In your click handler, instead of checking that.theObjects, check mySingleton.theObjects. Or, in mouseHandler change var that = this to var that = mySingleton.

    Edit: Or, pass the context to your anonymous function when you call it:

    mySingleton.mouseHandler = (function() {
        var that = this;
        return function (e) {
            for (var indx = 0; indx < that.theObjects.length; indx++) {
                // do something to that.theObjects[indx];
            }
        }
    }).call(mySingleton);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that had an inline member, but I later decided that
Previously, I had a class that wrapped an internal System.Collections.Generic.List<Item> (where Item is a
I have an application that uses CoreData. I previously had a class named Marker
I've messed with Access a little bit in the past, had one class on
Imagine in the Global.asax.cs file I had an instance class as a private field.
I had a winforms C# class that internally was looking up a file.. to
I have a class here that is defined like this: struct USERFPOINT { POINTFLOAT
I'm trying to recreate a solution that had multiple projects in it (only the
I had View strongly type of my class Usario Defined like this public partial
I'm working on a C++ project. I had a class with its function, then

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.