[英]Can Modules access variables stored in other modules that have not been imported, but both modules are exported to a main file?
I am a beginner in the world of JavaScript, and I am just learning about modules and how to use them. 我是JavaScript领域的初学者,我只是在学习有关模块以及如何使用它们的知识。
Here is the situation: 情况如下:
I have a main.js
file and two module#.js files, module1.js
and module2.js
. 我有一个
main.js
文件和两个module#.js文件, module1.js
和module2.js
。
project/
main.js
modules/
module1.js
module2.js
These files have both been individually imported into main.js
, but module2.js
contains a function displayAll()
that is meant to display all the items in an array items = [item1, item2, item3]
. 这些文件都已分别导入到
main.js
,但是module2.js
包含一个函数displayAll()
,用于显示数组中的所有项目items = [item1, item2, item3]
。
function displayAll() {
for (let i = 0; i < items.length; i++) {
console.log(`Item ${i} is ${items[i]}`)
}
}
This is not exactly how it looks, but that is beside the point. 这并不完全是它的外观,但这是重点。 My question is since the above function is contained in
module2.js
, how do let it access the array items
that is contained in module1.js
? 我的问题是, 由于上述函数包含在
module2.js
,如何让它访问module1.js
包含的数组items
? What if another file needed it? 如果需要另一个文件怎么办? How could I export it to multiple files without creating a messy web of imports?
我如何将其导出到多个文件而不创建混乱的导入网络?
I am not familiar with any frameworks yet, so please only use core javascript as much as possible. 我还不熟悉任何框架,因此请尽可能多地使用核心javascript。
Thanks! 谢谢!
First you need to create a function that loads what you need. 首先,您需要创建一个加载所需内容的函数。
In module2.js, you create this function that appends the module1.js to it 在module2.js中,创建此函数,将module1.js附加到该函数
function loadJS(callback) {
var firstScriptElem = document.getElementsByTagName('script')[0], script = document.createElement('script');
script.type = 'text/javascript';
script.src = module1.js;
script.async = false;
// Binds the event to the callback function. We use more than one event for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
firstScriptElem.appendChild(script);
};
You then need to create a callback function that actually runs the code you want to run after retrieving module1.js 然后,您需要创建一个回调函数,该函数实际上将在检索module1.js后运行您要运行的代码
function callBack() {
// get your array
// do something with the array
}
Now in module2 you call loadJS(callBack)
and there you have it. 现在在module2中,调用
loadJS(callBack)
并在其中找到它。
Now I know you are new and don't want to dive into frameworks yet. 现在,我知道您是新手,还不想深入研究框架。 But I should mention to you that requireJS does all of this very simply.
但是我要向您提及,requireJS非常简单地完成了所有这些工作。 I was using the kind of code above for a while but once I started using requireJS, I realize I should have used it from the beginning because its way more organized and you definitely don't have to repeat yourself with it.
我已经使用了一段时间,但是一旦我开始使用requireJS,我意识到我应该从一开始就使用它,因为它的组织方式更加合理,您当然不必重复它。 Makes module management easier (I too, only recently started learning the module patterns in javascript).
使模块管理更加容易(我也是,最近才开始学习javascript中的模块模式)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.