代码之家  ›  专栏  ›  技术社区  ›  ScottE

javascript对象文本:为什么我不能这样做?

  •  3
  • ScottE  · 技术社区  · 14 年前

    我有以下(简化的)对象文本。icons方法使用闭包来隐藏icons变量,我希望将其作为一个关联数组用于以后的查找。

    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;
                }
            };
    
        } ()
    }
    

    我将项添加到图标对象,如下所示:

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

    以下内容不起作用:

    allIcons[iconType] = icon;
    

    但是这样做:

    allIcons.push(icon);
    

    在闭包之外,关联数组样式工作正常,所以可能与jquery有冲突?我在Firebug中遇到的错误 A是未定义的 看来是从图书馆来的。我想保持关联数组样式。

    有什么想法吗?

    更新

    看起来这场冲突来自谷歌地图。奇怪,不知道怎么解决这个问题。

    哑巴更新

    返回基gicon()对象的对象文本部分根本没有返回对象。所以,对象没有正确的属性。

    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;
    }
    

    而mapListings.baseIcon与mapListings.baseIcon()不同!D'OH

    3 回复  |  直到 14 年前
        1
  •  4
  •   meder omuraliev    14 年前

    如果您想要查找表,只需 var allIcons = {}

    编辑: 尽管从技术上讲,它应该以任何方式工作,因为数组是一个对象。你确定没有别的吗?

    编辑第2页 :难道你不能把Allicon作为地图列表的属性吗?

    编辑3:我认为它起作用了,但也许你没有正确地访问它?或者它无法用谷歌创建对象,或者你发布的错误发生在其他地方,而不是这里。

    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'] )
    

    您不应该使用.length循环,而是直接访问 c7 c8 .

    x = MapListings.icons.get();
    for ( var prop in x ) {
        if ( x.hasOwnProperty(prop ) ) {
            alert( x[prop]['image'] )
        }
    }
    
        2
  •  1
  •   spinon    14 年前

    所以你可以做的一件事就是改变你引用数组的方式。由于添加方法是外部的,因此执行此操作:

    MapListings.icons["c7"]
    

    您也可以使用它将添加到添加函数内的数组中:

    add: function (iconType, iconImage) { 
        MapListings.icons[iconType] = iconImage;
    }, 
    
        3
  •  0
  •   Jacob    14 年前

    allIcons[iconType] = icon; 失败是因为 allIcons 是数组,而不是对象。尝试初始化 大力神 {} 相反。这将允许您按键在集合中放置项。