简体   繁体   English

模块可以访问未导入的其他模块中存储的变量,但两个模块都导出到主文件吗?

[英]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.jsmodule2.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.

相关问题 Node - 为什么导入的模块不能访问全局变量? - Node - Why can't imported modules access global variables? 如何在导入模块的其他功能中访问道具 - How to access props in other functions of imported modules 为什么在Firefox插件中,导入的对象无法访问导入者的导入模块? - Why in Firefox Addon the imported objects can not access the importers' imported modules? JavaScript:我可以访问包含导入模块的对象吗? - JavaScript: can I access the object that contains imported modules? Webpack 在使用索引文件导出和导入模块时不会拆分巨大的供应商文件 - Webpack doesn't split a huge vendor file when modules are exported and imported using index files 如何在ES6中访问导入模块的上下文? - How to access the context of imported modules in ES6? 从主控制器访问模块子模块服务 - Access modules child modules service from main controller 如何将所有导入的模块存储在一个文件中 - How to store all imported modules in one file javascript - 我们可以自我调用分配给变量的函数模块 - javascript - can we have self invoking function modules assigned to variables Mocha在导入的代码中找不到模块 - Mocha can't find modules in imported code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM