Javascript:在外部文件中声明数组 [英] Javascript: declaring an array in an external file

查看:127
本文介绍了Javascript:在外部文件中声明数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用extension.js在外部文件中声明一个数组,这样可以从主文件中访问它.有正确的方法吗?

Raymond Mercier

I want to declare an array in an external file, with extension.js, in such a way that this can be accessed from the main file. Is there a correct way ?

Raymond Mercier

推荐答案

您始终可以使用多个<script>标签在一个HTML文档中使用多个JavaScript文件;文件以自然顺序处理.您只需要考虑以下几点:加载页面时,不会执行<head>标记内部的脚本,但是会执行<body>标记内部的脚本.

JavaScript的执行从<body>标记下的脚本以及任何元素事件的事件处理程序开始.此时,可以使用已经加载的脚本的代码.

这样,所有文件都不是外部"文件.对于JavaScript,重要的是按照特定顺序定义执行顺序的声明集.

您可以在一个文件中定义一个数组,在另一个文件中使用它.您应该了解,JavaScript数组并不是真正的数组,因为它们在其他语言中也可以理解.而是,所有对象(不仅是数组,而且是所有对象)都是关联容器,其元素可通过任何键访问,这些键可以用作具有相似语法的数组索引.请参阅 http://www.w3schools.com/js/js_obj_array.asp [http://en.wikipedia.org/wiki/JavaScript [ ^ ].

通过这种方式,JavaScript中没有真实"数组.

有关HTML文件中执行顺序的更多详细信息,请参见:
http://en.wikipedia.org/wiki/JavaScript#Use_in_web_pages [ http://en.wikipedia.org/wiki/Unobtrusive_JavaScript [ http://stackoverflow.com/questions/708449/how-can-i- control-javascript-execution-order [ ^ ],
http://stackoverflow.com/questions/6396364/order-of-js- execute-in-html-pages [ ^ ],
http://stackoverflow.com/questions/2560025/javascript-execution-order [ ^ ],
http://stackoverflow.com/questions/5773976/javascript-jquery-execution-order-question [ ^ ].

—SA
You can always use multiple JavaScript files in one HTML document using multiple <script> tags; the files are processed in natural order. You only need to take into account the following: the scripts internal to the <head> tag are not executed when the page is loaded, but the scripts internal to the <body> tag are.

The execution of a JavaScript is started from the scripts placed under the <body> tag and any of the event handlers of the element events. At this moment, the code of already loaded scripts can be used.

In this way, none of the files is "external". For JavaScript, the important thing is just the set of declarations in certain order defining the order of execution.

You can define an array in one file, use it in another one. You should understand that the JavaScript arrays are not really the arrays as they are understood in other languages. Rather, all objects (not just arrays, but all objects) are associative containers with elements accessible by any keys which can be used as array indices, with similar syntax. Please see http://www.w3schools.com/js/js_obj_array.asp[^], http://en.wikipedia.org/wiki/JavaScript[^].

In this way, there are no "real" arrays in JavaScript.

For further detail on the order of execution in HTML file, please see:
http://en.wikipedia.org/wiki/JavaScript#Use_in_web_pages[^],
http://en.wikipedia.org/wiki/Unobtrusive_JavaScript[^].

See also questions and discussions on this topic:
http://stackoverflow.com/questions/708449/how-can-i-control-javascript-execution-order[^],
http://stackoverflow.com/questions/6396364/order-of-js-execution-in-html-pages[^],
http://stackoverflow.com/questions/2560025/javascript-execution-order[^],
http://stackoverflow.com/questions/5773976/javascript-jquery-execution-order-question[^].

—SA


这篇关于Javascript:在外部文件中声明数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆