简体   繁体   English

导出变量以便它可以从其他模块和全局范围访问的正确方法是什么?

[英]What is the proper way to export a variable so it can accessed from other modules and the global scope?

I was wondering about the proper way to export javascript and functions so they are accessable by other modules and also in the global scope.我想知道导出 javascript 和函数的正确方法,以便它们可以被其他模块访问,也可以在全局范围内访问。 For example I have this .js file with some utility functions.例如,我有这个带有一些实用功能的 .js 文件。

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

function random_choice(arr){
  let index = getRandomInt(0,arr.length);
  return arr[index];
}

function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

window.getRandomInt = getRandomInt;
window.random_choice = random_choice;
export {getRandomInt,random_choice,setCookie};

I found an answer on stackoverflow that said that to make variables and functions from modules accessible from the global scope (window) you can do window.VARIABLE = VARIABLE;我在 stackoverflow 上找到了一个答案,说要使模块中的变量和函数可以从全局范围(窗口)访问,您可以执行window.VARIABLE = VARIABLE; This works, but is that really how you are supposed to do it?这行得通,但这真的是你应该做的吗? What is the proper way to go about it if you have some variables or functions you want to be accessible from both other modules and in the window scope?如果您希望从其他模块和窗口范围内访问一些变量或函数,那么正确的方法是什么?

This works, but is that really how you are supposed to do it?这行得通,但这真的是你应该做的吗?

If you're using modules, assigning to the window is pretty weird - modules make dependencies between portions of your script explicit , which makes things MUCH more manageable when the scripts get large.如果您使用模块,分配给window非常奇怪 - 模块使脚本部分之间的依赖关系显式,当脚本变大时,这使得事情更易于管理。 In your case, consider whether there's any way you can refactor the code to avoid the global pollution - instead, have everything that needs getRandomInt to explicitly import it from this module.在您的情况下,请考虑是否有任何方法可以重构代码以避免全局污染 - 相反,拥有需要getRandomInt从该模块显式导入它的所有内容。

That said, assigning to the window isn't completely unheard of.也就是说,分配给窗口并非完全闻所未闻。 It's common for libraries to assign a big namespace object to the window.库为窗口分配一个大的命名空间对象是很常见的。 For example, I believe jQuery does something like:例如,我相信 jQuery 会做类似的事情:

const jQuery = /* lots of code */;
export default jQuery;
window.$ = jQuery;

But usually that sort of thing is only done once, at most.但通常这种事情最多只做一次。

If you have a bunch of assorted helper functions that get called from lots and lots of places all over your script, and you aren't happy with importing them everywhere, you could make a global helpers object:如果你有一堆各种各样的辅助函数,它们在你的脚本的很多地方被调用,并且你不喜欢在任何地方导入它们,你可以创建一个全局 helpers 对象:

window.shivanHelpers = {
  getRandomInt,
  random_choice,
};

But IMO you should first consider whether it's absolutely necessary.但是IMO你应该首先考虑它是否绝对必要。 Usually, it isn't.通常,它不是。

if you use this js file in browser, window is a place to hold global variable;如果你在浏览器中使用这个js文件, window就是一个存放全局变量的地方; even you use var x = 1;即使你使用var x = 1; not in any scope, it will automatically bind it to window as window.x = 1 .不在任何范围内,它会自动将其绑定到 window 作为window.x = 1 if you use this js file in nodejs in backend, you may use global or use process.env to store global variable or if it is a module, you can also use module.exports .如果你在后端的 nodejs 中使用这个 js 文件,你可以使用global或使用process.env来存储全局变量,或者如果它是一个模块,你也可以使用module.exports

if you want to hide sth you can try this form:如果你想隐藏某物,你可以试试这个表格:

// set global = window / global, related to your runtime env
(function () {
   var hidden_var = 1;
   var export_var = 2;
   global.export_var = export_var;
})();
// then outside the function,
// you can use global.export_var to get the export_var
// but hidden_var is not available outside the function.

// if you do not want to use it any more
delete global.export_var;

so the proper way depends on your decision;所以正确的方法取决于你的决定; as long as the data are accessible and no leading to memory leak (set anywhere and cleanup when it is time)只要数据可以访问并且不会导致内存泄漏(在任何地方设置并在适当的时候清理)

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

相关问题 为什么在这种情况下无法访问全局scope? - Why global scope can not be accessed in this case? 在 ES6 模块中定义全局变量的正确方法是什么? - What is the correct way to define global variable in ES6 modules? map integer 到 Javascript 中其他变量的正确方法是什么 - What is proper way to map integer to other variable in Javascript 无法访问另一个文件中的全局变量 - Global variable in another file can't be accessed 如何在一个可以被其他函数(JavaScript)访问的函数中定义全局变量? - How do I define a global variable in a function that can be accessed by other functions (JavaScript)? 导出 function 中的变量以用于其他模块 - export a variable inside a function to be used in other modules 如何将匿名函数内的变量导出到JavaScript中的全局作用域? - How can I export a variable inside of an anonymous function to the global scope in JavaScript? 在Javascript中包含模块的正确方法是什么? - What is the proper way to include modules in Javascript? 正确的ES6与子模块共享全局方式 - Proper es6 way of sharing global with sub-modules webpack 创建并导出一个全局 object ,其中包含要被其他文件访问的全局变量 - webpack create and export a global object which contains global variables to be accessed by other files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM