[英]How to call one javascript variable to another javascript file
[英]How to call one variable in a JavaScript file to another JavaScript file?
目前,我有以下幾行代碼。 這是存儲在名為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文件中,我嘗試從RealMapofIreland.js調用變量“ Real”
var polyline = L.polyline(
Real,
{ color: 'red', weight: 2 }
).addTo(map);
然后在主頁中,我都調用了兩個JavaScript文件:
<script src="js/RealMapofIreland.js"></script>
<script src="js/LeafletMap.js"></script>
這是行不通的。 我假設當在主頁中RealMapodIreland.js
的Real
, RealMapodIreland.js
將Real
變量數據傳遞給LeafletMap.js
折線函數。
我是否必須在另一個JavaScript文件中調用一個JavaScript文件,或者是否有更簡單的方法來執行此操作?
定義不帶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]
];
如您的描述,這應該起作用。
在調用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);
首先,要確保文件以正確的順序加載,請使用“ defer”屬性。 <script defer src="js/RealMapofIreland.js"></script> <script defer src="js/LeafletMap.js"></script>
嘗試為每個包含的文件創建窗口屬性數據。
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] ];
在“ LeafletMap.js”中
var polyline = L.polyline(
window.RealMap.Real,
{ color: 'red', weight: 2 }
).addTo(map);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.