如何将JavaScript文件中的一个变量调用到另一个JavaScript文件?

时间:2014-07-28 13:27:02

标签: javascript leaflet

目前我有以下几行代码。 这是存储在名为RealMapofIreland.js

的JavaScript文件中的变量中的坐标数据
var Real = [

        [53.286, -5.796], [53.140, -5.778], [52.525, -5.745], [52.152, -6.108], [51.756, -6.459], [51.481, -6.833], [50.987, -7.481], [50.812, -8.470],
        [50.361, -8.668], [50.097, -9.151], [50.217, -9.536], [48.373, -11.469], [48.10009, -11.85785], [49.1027, -13.27869], [49.38237, -14.06971],
        [51.50874, -16.36208], [51.86292, -15.74684], [52.1874, -15.74684], [52.42922, -18.5154], [55.02802, -19.26247], [55.14121, -21.12293],
        [55.5535, -21.59157], [56.897, -20.77103], [56.70451, -18.86696], [56.60789, -18.92206], [56.48676, -17.81227], [56.4382, -17.79416],
        [56.40782, -16.71381], [56.38198, -16.71111], [56.33481, -15.87885], [56.27996, -15.87705], [56.23114, -14.87909], [56.18225, -14.87729],
        [56.18225, -14.16318], [56.15167, -14.07529], [56.12412, -13.70725], [56.10575, -13.70315], [56.08277, -13.19273], [56.05517, -13.20921],
        [55.628, -7.42491], [55.6311, -7.28484], [55.61559, -7.26836], [55.62334, -7.13927], [55.61249, -7.13193], [55.61249, -6.92319],
        [55.33539, -6.77123]

];

在另一个名为LeafletMap.js的Javascript文件中,我尝试调用变量' Real'来自RealMapofIreland.js

var polyline = L.polyline(
                  Real,
                  { color: 'red', weight: 2 }
               ).addTo(map);

然后在主页面中我调用两个JavaScript文件:

<script  src="js/RealMapofIreland.js"></script>
<script src="js/LeafletMap.js"></script>

这不起作用。我假设在主页面中调用的位置Real中的RealMapodIreland.js都会将Real变量数据传递给LeafletMap.js中的折线函数。

我是否必须在另一个JavaScript文件中调用一个JavaScript文件,或者是否有更简单的方法来执行此操作?

3 个答案:

答案 0 :(得分:0)

定义没有var的变量。如果使用var定义它,它将变为对该文件的私有,并且不是全局的。

Real = [

        [53.286, -5.796], [53.140, -5.778], [52.525, -5.745], [52.152, -6.108], [51.756, -6.459], [51.481, -6.833], [50.987, -7.481], [50.812, -8.470],
        [50.361, -8.668], [50.097, -9.151], [50.217, -9.536], [48.373, -11.469], [48.10009, -11.85785], [49.1027, -13.27869], [49.38237, -14.06971],
        [51.50874, -16.36208], [51.86292, -15.74684], [52.1874, -15.74684], [52.42922, -18.5154], [55.02802, -19.26247], [55.14121, -21.12293],
        [55.5535, -21.59157], [56.897, -20.77103], [56.70451, -18.86696], [56.60789, -18.92206], [56.48676, -17.81227], [56.4382, -17.79416],
        [56.40782, -16.71381], [56.38198, -16.71111], [56.33481, -15.87885], [56.27996, -15.87705], [56.23114, -14.87909], [56.18225, -14.87729],
        [56.18225, -14.16318], [56.15167, -14.07529], [56.12412, -13.70725], [56.10575, -13.70315], [56.08277, -13.19273], [56.05517, -13.20921],
        [55.628, -7.42491], [55.6311, -7.28484], [55.61559, -7.26836], [55.62334, -7.13927], [55.61249, -7.13193], [55.61249, -6.92319],
        [55.33539, -6.77123]

];

答案 1 :(得分:0)

根据你的描述,这应该有效。

请在调用L.ployline函数之前检查RealMapofIreland.js是否已加载。要检查Real变量是否已加载,请在L.ployline:

之前添加以下行
console.log(Real);

然后打开浏览器的开发工具并重新加载页面,您应该观察Real值是否已记录。应注意任何警告,如未定义的变量。

如果是,但这也不起作用,请将RealMapofIreland.js更改为:

window.Real = [ ..... ];

而L.ployline的功能如下:

var polyline = L.polyline(
                  window.Real,
                  { color: 'red', weight: 2 }
               ).addTo(map);

答案 2 :(得分:0)

  1. 首先,要确保以正确的顺序加载文件,请使用“defer”属性。 <script defer src="js/RealMapofIreland.js"></script> <script defer src="js/LeafletMap.js"></script>

  2. 尝试为每个包含的文件创建窗口属性数据。

    window.RealMap = {};window.RealMap.Real = [   
                [53.286, -5.796], [53.140, -5.778], [52.525, -5.745], [52.152, -6.108], [51.756, -6.459], [51.481, -6.833], [50.987, -7.481], [50.812, -8.470],
                [50.361, -8.668], [50.097, -9.151], [50.217, -9.536], [48.373, -11.469], [48.10009, -11.85785], [49.1027, -13.27869], [49.38237, -14.06971],
                [51.50874, -16.36208], [51.86292, -15.74684], [52.1874, -15.74684], [52.42922, -18.5154], [55.02802, -19.26247], [55.14121, -21.12293],
                [55.5535, -21.59157], [56.897, -20.77103], [56.70451, -18.86696], [56.60789, -18.92206], [56.48676, -17.81227], [56.4382, -17.79416],
                [56.40782, -16.71381], [56.38198, -16.71111], [56.33481, -15.87885], [56.27996, -15.87705], [56.23114, -14.87909], [56.18225, -14.87729],
                [56.18225, -14.16318], [56.15167, -14.07529], [56.12412, -13.70725], [56.10575, -13.70315], [56.08277, -13.19273], [56.05517, -13.20921],
                [55.628, -7.42491], [55.6311, -7.28484], [55.61559, -7.26836], [55.62334, -7.13927], [55.61249, -7.13193], [55.61249, -6.92319],
                [55.33539, -6.77123]
    ];
    
  3. 在'LeafletMap.js'中

    var polyline = L.polyline(
                      window.RealMap.Real,
                      { color: 'red', weight: 2 }
                   ).addTo(map);