[英]A local/global variable in RequireJS
I'd like to create a variable that will be accessible from any module inside my RequireJS project. 我想创建一个变量,该变量可从RequireJS项目内的任何模块访问。
For example, on init I'd like to set: 例如,在初始化时,我想设置:
this.myVar = 123;
and be able to access it in any other module inside (but not outside) my RequireJS project such as: 并能够在我的RequireJS项目内部(但不在外部)的任何其他模块中访问它,例如:
console.log(this.myVar);
Is this possible? 这可能吗?
PS - I'm using 'this' just for an example. PS-我仅以“ this”为例。 Any other option is viable.
任何其他选择都是可行的。
PS 2 - The RequireJS is actually a widget that can be instantiated several times such as : PS 2-RequireJS实际上是一个可以实例化多次的小部件,例如:
new Widget('w1');
new Widget('w2');
and the global parameter should be inside each instance and not shareable between them. 并且全局参数应该在每个实例内部,并且不能在它们之间共享。
EDIT (Example for such possible code, which doesn't work): 编辑(此类可能的代码示例,该代码无效):
define('module1', [], function() {
var myVar = 123;
});
define('module2', ['module1'], function(m1) {
console.log(myVar); // Should print '123'
});
创建一个Settings
模块,任何需要访问设置的人都会导入它-让它导出具有所需变量的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.