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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:19:32+00:00 2026-05-15T18:19:32+00:00

I have the following (simplified) object literal. The icons method uses closure to hide

  • 0

I have the following (simplified) object literal. The icons method uses closure to hide the icons variable, which I’d like to have as an associative array for later lookups.

var MapListings = {
    icons: function () {
        var allIcons = [] ;

        return {
            add: function (iconType, iconImage) {
                var icon = new GIcon(MapListings.baseIcon);
                icon.image = iconImage;
                allIcons[iconType] = icon; // fails, but this is what I want
                // allIcons.push(icon); // works, but this is not what I want
            },
            get: function () {
                return allIcons;
            }
        };

    } ()
}

I add items to the to the icons object like so:

MapListings.icons.add("c7", "/images/maps/blue.png");
MapListings.icons.add("c8", "/images/maps/red.png");

The following doesn’t work:

allIcons[iconType] = icon;

But this does:

allIcons.push(icon);

Outside of the closure the associative array style works fine, so perhaps there is a conflict with jQuery? The error I get in firebug a is undefined looks to come from the library. I’d like to maintain the associative array style.

Any ideas?

Update

It looks like this conflict is coming from google maps. Odd, not sure of a way around this.

Dumbass Update

The part of my object literal that returned a base GIcon() object wasn’t returning an object at all. So, the object didn’t have the right properties.

baseIcon: function () {
    var base = new GIcon();
    base.shadow = '/images/maps/shadow.png';
    base.iconSize = new GSize(12, 20);
    base.shadowSize = new GSize(22, 20);
    base.iconAnchor = new GPoint(6, 20);
    base.infoWindowAnchor = new GPoint(5, 1);
    return base;
}

And MapListings.baseIcon is NOT the same as MapListings.baseIcon()! D’oh

  • 1 1 Answer
  • 1 View
  • 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-15T18:19:33+00:00Added an answer on May 15, 2026 at 6:19 pm

    if you want a lookup table, just do var allIcons = {}

    EDIT: Though technically it should work either way, as an array IS an object. Are you sure there isn’t more to this?

    EDIT #2: Can’t you just make allIcons as a property of MapListings?

    EDIT #3: I think it’s working, but maybe you’re not accessing it right? That or it fails creating the object with Google somehow, or the error you posted is happening elsewhere, and not here

    function GIcon(){};
    var MapListings = {
        icons: function () {
            var allIcons = [] ;
    
            return {
                add: function (iconType, iconImage) {
                    var icon = new GIcon(MapListings.baseIcon);
                    icon.image = iconImage;
                    allIcons[iconType] = icon; // fails, but this is what I want
                    // allIcons.push(icon); // works, but this is not what I want
                    window.x = allIcons
                },
                get: function () {
                    return allIcons;
                }
            };
    
        } ()
    };
    
    MapListings.icons.add("c7", "/images/maps/blue.png");
    MapListings.icons.add("c8", "/images/maps/red.png");
    
    alert( MapListings.icons.get()['c8']['image'] )
    

    You shouldn’t loop using .length but instead directly access c7 or c8.

    x = MapListings.icons.get();
    for ( var prop in x ) {
        if ( x.hasOwnProperty(prop ) ) {
            alert( x[prop]['image'] )
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following setup (simplified, obviously): An abstract class, with an A object
We have a legacy COM DLL with the following simplified IDL for some method:
In a Javascript program I have an object with the following two (simplified) functions:
So, I have the following object (simplified for sake of example): public class SomeListener
I have the following (obviously simplified) class class A(object) def __init__(self, a): self.a =
I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #...
Currently I have code like the following (simplified somewhat). Eventually, I've added more and
I have the following simplified ViewModel public class UserViewModel : IUserViewModel { public DelegateCommand<object>
I have the following code, which loops through an array of menuoptions and on
If I have created the following Employee object (simplified)... public class Employee { public

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.