Javascript 从另一个js文件获取数组项

Javascript 从另一个js文件获取数组项,javascript,jquery,arrays,jquery-mobile,Javascript,Jquery,Arrays,Jquery Mobile,我的课程结构如下: index.html: <html> <head> <script src="first.js"></script> <script src="second.js"></script> </head> ...</html> second.js: console.log(array); 在first.js中,我将一些对象推送到数组中,但是seco

我的课程结构如下:

index.html:

<html>
   <head>
      <script src="first.js"></script>
      <script src="second.js"></script>
  </head>
...</html>
second.js:

console.log(array);
在first.js中,我将一些对象推送到数组中,但是second.js中的console.log说,我的数组是空的。我做错了什么?谢谢…

您的
控制台.log(array)
很可能在文档准备就绪之前被调用,此时您的主应用程序代码正在运行


在将数据添加到阵列后,您应该将
console.log
移动到主文件中,或者在主功能完成后使用回调或事件来记录它

如前几位用户所述,在文档完全加载后填充阵列时,您的console.log将立即运行。但是,即使在加载文档时运行console.log,它仍然看不到您的变量。您要做的是,在“点击”事件中,将数组发送到second.js中定义的函数,如下所示:

在first.js中

"use strict";
var array = [];

$(document).ready(function () {

$("#xy").on("tap", function () {
   array.push(new arrayItem());
   someFunction(array);
}
在second.js中

function someFunction(array) {
    console.log(array);
    // Do the rest of your code that requires 'array' here
}

这样,每次触发on-tap事件时,都会向数组传递某个函数

是否确实在触发
tap
事件后进行日志记录?在文档准备就绪之前,不会执行First。Second正在立即执行。我的数组的条目应该在Second.js中可用。我如何使用回调来管理它呢?看起来好像其他人刚刚发布了一个这样的示例!
function someFunction(array) {
    console.log(array);
    // Do the rest of your code that requires 'array' here
}