|
|
要出家的感冒药 · 元素隐式具有 "any" 类型,因为类型为 ...· 2 年前 · |
|
|
坚强的香蕉 · Python 在办公自动化中的简单应用 - 少数派· 2 年前 · |
|
|
打酱油的稀饭 · 【java】获取属性名工具 - ...· 2 年前 · |
|
|
爽快的菠菜 · 解析python中的jsonpath 提取器 ...· 3 年前 · |
这里有两个文件:
// 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'));
|
|
要出家的感冒药 · 元素隐式具有 "any" 类型,因为类型为 "string" 的表达式不能用于索引类型 "{}"。 在类型 "{}" 上找不到具有类型为 "string" 的参数的索引签名。ts(7053) - 2 年前 |
|
|
坚强的香蕉 · Python 在办公自动化中的简单应用 - 少数派 2 年前 |