简体   繁体   English

Javascript对象字面量:为什么我不能这样做?

[英]Javascript object literal: why can't I do this?

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

    } ()
}

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? 在闭包之外,关联数组样式工作正常,所以可能与jQuery存在冲突? The error I get in firebug a is undefined looks to come from the library. 我在firebug中得到的错误是未定义的看起来来自库。 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 Dumbass更新

The part of my object literal that returned a base GIcon() object wasn't returning an object at all. 返回基础GIcon()对象的对象文字部分根本没有返回对象。 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()! MapListings.baseIcon与MapListings.baseIcon()不同! D'oh D'哦

if you want a lookup table, just do var allIcons = {} 如果你想要一个查找表,只需要做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? 编辑#2 :难道你不能将allIcons作为MapListings的属性吗?

EDIT #3: I think it's working, but maybe you're not accessing it right? 编辑#3:我认为它有效,但也许你没有正确访问它? That or it fails creating the object with Google somehow, or the error you posted is happening elsewhere, and not here 那或者它无法以某种方式与Google创建对象,或者您发布的错误发生在其他地方,而不是在这里

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 . 你不应该使用.length循环,而是直接访问c7c8

x = MapListings.icons.get();
for ( var prop in x ) {
    if ( x.hasOwnProperty(prop ) ) {
        alert( x[prop]['image'] )
    }
}

So one thing you could do to fix this is change the way you reference the array. 因此,您可以做的一件事就是更改引用数组的方式。 Since external to your add method you do this: 从你的add方法外部你可以这样做:

MapListings.icons["c7"]

You can also just use this to add to your array inside your add function: 您也可以使用它来添加到add函数中的数组:

add: function (iconType, iconImage) { 
    MapListings.icons[iconType] = iconImage;
}, 

allIcons[iconType] = icon; fails because allIcons is an Array, not an object. 失败,因为allIcons是一个数组,而不是一个对象。 Try initializing allIcons to {} instead. 尝试将allIcons初始化为{} That would allow you to place items in the collection by key. 这样您就可以按键将项目放入集合中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM