![]() |
挂过科的熊猫 · 王艺禅颠覆《等到胜利那一天》四大卫视同播-搜狐娱乐· 10 月前 · |
![]() |
不拘小节的酸菜鱼 · 瑞安市人民政府联系方式· 11 月前 · |
![]() |
机灵的草稿本 · 黄达|《中国金融》70年• ...· 11 月前 · |
![]() |
聪明伶俐的蛋挞 · 中国驻希腊大使田学军陪同铁道部部长刘志军与希 ...· 1 年前 · |
![]() |
逃课的日光灯 · 专访飞机设计专家李光哲_航空产业_中国经济网· 1 年前 · |
这里有两个文件:
// main.js
require('./modules');
console.log(name); // prints "foobar"
// module.js
name = "foobar";
当我没有"var“时,它可以工作。但当我有了:
// module.js
var name = "foobar";
名称在main.js中将是未定义的。
我听说全局变量是不好的,你最好在引用前使用"var“。但是,这是一个全局变量很好的情况吗?
使用或不使用var关键字声明的变量被附加到全局对象。这是在Node中通过声明不带var关键字的变量来创建全局变量的基础。而使用var关键字声明的变量仍然是模块的局部变量。
请参阅本文以进一步了解- https://www.hacksparrow.com/global-variables-in-node-js.html
将任何想要共享的变量保存为一个对象。然后将其传递给已加载的模块,以便它可以通过对象引用访问变量。
// main.js
var myModule = require('./module.js');
var shares = {value:123};
// Initialize module and pass the shareable object
myModule.init(shares);
// The value was changed from init2 on the other file
console.log(shares.value); // 789
在另一个文件上..
// module.js
var shared = null;
function init2(){
console.log(shared.value); // 123
shared.value = 789;
module.exports = {
init:function(obj){
// Save the shared object on current module
shared = obj;
// Call something outside
init2();
}
如果我们需要共享多个变量,请使用以下格式
//module.js
let name='foobar';
let city='xyz';
let company='companyName';
module.exports={
name,
city,
company
}
用法
// main.js
require('./modules');
console.log(name); // print 'foobar'
持不同意见的人,我认为如果您要将代码发布到
npm
,
global
变量可能是最好的选择,因为您不能确保所有包都使用相同版本的代码。所以如果你使用一个文件来导出一个
singleton
对象,它会在这里引起问题。
您可以选择
global
、
require.main
或跨文件共享的任何其他对象。
否则,将包安装为可选的依赖包可以避免此问题。
如果有更好的解决方案,请告诉我。
这不是一种新的方法,而是经过了一些优化。创建一个包含全局变量的文件,并通过
export
和
require
共享它们。在本例中,Getter和Setter更具动态性,全局变量可以是只读的。要定义更多的全局变量,只需将它们添加到
globals
对象。
global.js
const globals = {
myGlobal: {
value: 'can be anytype: String, Array, Object, ...'
aReadonlyGlobal: {
value: 'this value is readonly',
protected: true
dbConnection: {
value: 'mongoClient.db("database")'
myHelperFunction: {
value: function() { console.log('do help') }
exports.get = function(global) {
// return variable or false if not exists
return globals[global] && globals[global].value ? globals[global].value : false;
exports.set = function(global, value) {
// exists and is protected: return false
if (globals[global] && globals[global].protected && globals[global].protected === true)
return false;
// set global and return true
globals[global] = { value: value };
return true;
};
在any-other-file.js中获取和设置的示例
const globals = require('./globals');
console.log(globals.get('myGlobal'));
// output: can be anytype: String, Array, Object, ...
globals.get('myHelperFunction')();
// output: do help
let myHelperFunction = globals.get('myHelperFunction');
myHelperFunction();
// output: do help
console.log(globals.set('myGlobal', 'my new value'));
// output: true
console.log(globals.get('myGlobal'));
// output: my new value
console.log(globals.set('aReadonlyGlobal', 'this shall not work'));
![]() |
挂过科的熊猫 · 王艺禅颠覆《等到胜利那一天》四大卫视同播-搜狐娱乐 10 月前 |
![]() |
不拘小节的酸菜鱼 · 瑞安市人民政府联系方式 11 月前 |
![]() |
机灵的草稿本 · 黄达|《中国金融》70年• 70人特别谈_手机搜狐网 11 月前 |
![]() |
逃课的日光灯 · 专访飞机设计专家李光哲_航空产业_中国经济网 1 年前 |