【问题标题】:How do I access object parameter passed in a function using javascript?如何使用javascript访问函数中传递的对象参数?
【发布时间】:2013-10-22 07:54:58
【问题描述】:

如何使用 javascript 访问函数中传递的对象参数? 我想动态使用各种 column_name。 feature.attribute 有列名。我想将 feature.attribute列名 连接起来。到目前为止,我已经尝试过:

我的代码:

var column_name = "LOCAL_POP";

var initialize = {
    init_style: function() {
        style = new OpenLayers.StyleMap({
            "default": new OpenLayers.Style(
                    {
                        fillColor: "${getColor}",
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7,
                        graphicZIndex: 1,
                        label: "${DISTRICT}"
                    },
            {
                context: {
                    getColor: function(feature) {

**// tested by creating a local variable and window variable**

                        this.feature = feature;
                        var feature_name = 'feature.attributes.' + column_name;
                        console.log(window);
                        console.log(this['feature_name']);
                        console.log(window['feature_name']);
                        return  feature_name > 1000000 ? '#006D2C' :
                                feature_name > 100000 ? '#31A354' :
                                feature_name > 5000 ? '#74C476' :
                                feature_name >= 0 ? '#A1D99B' :
                                '';
                    }
                }
            }),
            "select": new OpenLayers.Style(
                    {
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7
                    })
        });
        return style;
    }
};

【问题讨论】:

  • 你在问题​​中提到的函数在哪里?
  • @BeNdErR initialize.init_style 的上下文函数。
  • 那么您是否尝试动态访问feature.attributes.randompropertyname 之类的内容?
  • @Qantas94Heavy 是的。我是。

标签: javascript openlayers


【解决方案1】:

您的财产访问权限错误。要动态访问对象的属性,请使用不带引号的方括号表示法(这使其成为字符串而不是您想要的变量):

feature.attributes[column_name]

这是您的固定代码:

var column_name = "LOCAL_POP";

var initialize = {
    init_style: function() {
        style = new OpenLayers.StyleMap({
            "default": new OpenLayers.Style(
                    {
                        fillColor: "${getColor}",
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7,
                        graphicZIndex: 1,
                        label: "${DISTRICT}"
                    },
            {
                context: {
                    getColor: function(feature) {

**// tested by creating a local variable and window variable**

                        this.feature = feature;
                        var feature_name = feature.attributes[column_name];
                        return  feature_name > 1000000 ? '#006D2C' :
                                feature_name > 100000 ? '#31A354' :
                                feature_name > 5000 ? '#74C476' :
                                feature_name >= 0 ? '#A1D99B' :
                                '';
                    }
                }
            }),
            "select": new OpenLayers.Style(
                    {
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7
                    })
        });
        return style;
    }
};

【讨论】:

  • 你很棒。谢谢。
猜你喜欢
  • 2019-10-28
  • 1970-01-01
  • 2013-10-06
  • 2018-06-25
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
相关资源
最近更新 更多