NodeJS Variable Scope: Preventing accessing global variables

送分小仙女□ 提交于 2019-12-25 07:58:47

问题


Let's say I got a global variable called myData. It was declared as myData = 1; at the beginning of a script.

Goal:

I want to create a single module that will not have access this myData in any way. myData must remain global in other modules.

Note: Yes, I already do know that I could require myData in every page where I need it but that's not what I'm looking for.


Attempts:

To do that, at the very beginning of the module, I wrote: var myData;. The module could no longer access myData directly by myData.

Problem: You can still access it via GLOBAL.myData

So I instead, I wrote var myData, GLOBAL; at the beginning of the module.


Are there any other ways someone could access myData? If so, how could I prevent it?

来源:https://stackoverflow.com/questions/23177039/nodejs-variable-scope-preventing-accessing-global-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!