从另一个单独的javascript函数访问值

时间:2014-01-10 10:01:09

标签: javascript jquery

我有几个重复的Highchart线图使用相同的vars配置。

graph.js

 function getColor(){
    var getColor ={
    facebook: "#3d599b",
    twitter: "#00abee",
    instagram: "#517fa4",
    soundcloud: "#fa3d00",
    youtube: "#d43c3b",
    tumblr: "#3d5a71",
    vine: "#00b589",
    foursquare: "#0abadf",
    linkedin: "#007ab9",
    vimeo: "#1ab7ea",
    fivehundredpx: "#000000",
    wordpress: "#257ba0",
    rdio: "#028ed4",
    behance: "#000000",
    flickr: "#ff0084",
    google: "#e52d27",
    facebook_page: "#3d599b"
    }; 
  }

new_graph.js

 var seriesOptions = [],
        yAxisOptions = [],
        seriesCounter = 0,
        names = <%= raw get_engagement_data(@user,params[:provider],params[:type]) %>;

  $.each(names, function(i, name) {

    seriesOptions[i] = {
        type: 'areaspline',
        lineColor: getColor[name[2]],
        lineWidth: 2,
        pointInterval: 24 * 3600 * 1000,
        name: name[0],
        data: name[1],
        fillColor: "#000000",
        fillOpacity: 1,
        marker: {
            enabled: false,
            fillColor: getColor[name[2]]
        }
    };

    // As we're loading the data asynchronously, we don't know what order it will arrive. So
    // we keep a counter and create the chart when all the data is loaded.
    seriesCounter++;

    if (seriesCounter == names.length) {
        createChart();
    }
});

尝试从diff中访问来自getColor函数的变量。 javascript文件但没有用。

1 个答案:

答案 0 :(得分:0)

将您的功能更改为:

    function getColor(nam)
    {
        var colors ={
        facebook: "#3d599b",
        twitter: "#00abee",
        instagram: "#517fa4",
        soundcloud: "#fa3d00",
        youtube: "#d43c3b",
        tumblr: "#3d5a71",
        vine: "#00b589",
        foursquare: "#0abadf",
        linkedin: "#007ab9",
        vimeo: "#1ab7ea",
        fivehundredpx: "#000000",
        wordpress: "#257ba0",
        rdio: "#028ed4",
        behance: "#000000",
        flickr: "#ff0084",
        google: "#e52d27",
        facebook_page: "#3d599b"
        };
    return colors[nam];
  }