简体   繁体   English

无法从另一个JavaScript文件引用JavaScript数组

[英]Unable to refer javascript array from another javascript file

I am trying to create a chart using highcharts in which i am not able to fetch data which i store in another js file. 我正在尝试使用highcharts创建图表,在该图表中我无法提取存储在另一个js文件中的数据。

My main.js file has all the code for creating chart. 我的main.js文件包含用于创建图表的所有代码。 It has the section of series. 它具有系列的部分。

series: [{
            name: 'Desktops',
            data: '/data/desktop.js',
            tooltip: {
                valueDecimals: 2
            }
        }]

Here i want to refer data from another js file. 在这里,我想引用另一个js文件中的数据。 The name of file is desktop.js and it has just the below array 文件的名称是desktop.js,它具有以下数组

var desktopData = [[1475272800000, 117759], [1475359200000, 106147], [1475445600000, 147747], [1475532000000, 302031], [1475618400000, 520539], [1475704800000, 245353], [1475791200000, 180376], [1475877600000, 78819], [1475964000000, 90466], [1476050400000, 257822], [1476136800000, 284465], [1476223200000, 242898], [1476309600000, 297186], [1476396000000, 268069], [1476482400000, 183149], [1476568800000, 410442], [1476655200000, 1117798], [1476741600000, 1274668], [1476828000000, 1331799], [1476914400000, 1230213], [1477000800000, 888251], [1477087200000, 572050], [1477173600000, 931144], [1477260000000, 1556641], [1477346400000, 1526736], [1477432800000, 1310133], [1477519200000, 1207422], [1477605600000, 785556], [1477692000000, 487264], [1477778400000, 787714], [1477868400000, 942663]];

How can i refer array defined in another file? 如何引用另一个文件中定义的数组?

Do you use any type of the framework? 您是否使用任何类型的框架? To load JS file in another file, you need to require it somehow, either by using AMD, ES imports or any other way (be it synchronous or asynchronous). 要将JS文件加载到另一个文件中,您需要通过使用AMD,ES导入或任何其他方式(同步或异步)以某种方式要求它。

If you are just adding the files to the HTML (using script tags), then the only thing you have to do is to make sure that the files are loaded ( DOMContentLoaded event) and then, you have to attach the data to a global scope. 如果您只是将文件添加到HTML(使用脚本标签),那么唯一要做的就是确保文件已加载( DOMContentLoaded事件),然后将数据附加到全局范围。

Global scope in the browsers is called a window and every time when you use variable that was not declared in your scope by var it means that the JS is searching for it in the global scope. 浏览器中的全局作用域称为window ,每次使用var尚未在作用域中声明的变量时,这都意味着JS在全局作用域中进行搜索。 In order to assign something to a global scope you can just omit var or explicitely assign it like this: 为了将某些东西分配给全局范围,您可以省略var或显式分配它,如下所示:

window.desktopData = [];

// And then in another file, assuming that the data is ready

var myData = window.desktopData

Please also remember that including files in <script> tags usually means that they will block your browser and the order of the files matters (at least when you want to rely on the fact of one file already loaded before another file). 还请记住,在<script>标记中包含文件通常意味着它们将阻止您的浏览器,并且文件的顺序很重要(至少在您要依赖一个文件已经在另一个文件之前加载的事实时)。 I'd suggest using some kind of import system then: 我建议然后使用某种导入系统:

http://requirejs.org/docs/whyamd.html http://requirejs.org/docs/whyamd.html

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

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