tht*_*gma 37 variables module sharing node.js total.js
var count = 1;
// psuedocode
// if (words typed begins with @add)
require('./add.js');
// if (words typed begins with @remove)
require('./remove.js');
// if (words typed begins with @total)
require('./total.js');
module.exports.count = count;
Run Code Online (Sandbox Code Playgroud)
var count = require('./main.js').count;
console.log(count);
Run Code Online (Sandbox Code Playgroud)
var count = require('./main.js').count;
count += 10;
console.log(count);
Run Code Online (Sandbox Code Playgroud)
var count = require('./main.js').count;
count -= 10;
console.log(count);
Run Code Online (Sandbox Code Playgroud)
1
11
-9
Run Code Online (Sandbox Code Playgroud)
我有一个应用程序(irc bot),我想添加一个窥视可以执行@add 1或@remove 1的功能.我有一个main.js然后需要不同的文件,具体取决于所说的触发器.因此add会触发add.js文件,然后需要('main.js')并添加10(简化为10,它实际上会解析数字并使用该数字).我遇到的问题是当有人去做并且做@remove时.它需要('main.js')并从1减去10,结果为-9.做@total会输出1.
我已经对module.exports进行了相当不错的搜索,我没有遇到像上面列出的那个例子. 文档中没有任何与我想要做的相近的例子; 而这些问题1,2我的理解-但任何用处我不是-我的理解什么正在那里说.
我想同时让@add和@remove操纵相同的变量(count),并且@total要将@add和@removes考虑在内的总计数返回.我是否正确使用module.exports; 或者是否有共享变量的常用方法,一个文件能够修改module.exports的内容并将结果返回到main.js文件?
Min*_*God 51
您的问题是,当您这样做时var count = require('./main.js').count;,您会得到该号码的副本,而不是参考号.改变count不会改变"来源".
但是,您应该具有文件导出功能.需要一个文件只会在第一次运行它,但之后它会被缓存而不会重新运行.看文档
建议#1:
// main.js
var count = 1;
var add = require('./add.js');
count = add(count);
// add.js
module.exports = function add(count) {
return count+10;
}
Run Code Online (Sandbox Code Playgroud)
#2:
var count = 1;
var add = function() {
count += 10;
}
add();
Run Code Online (Sandbox Code Playgroud)
#3:我个人会创建一个计数器模块(这是一个单独的实例,但你可以很容易地把它变成一个"类"):
// main.js
var counter = require('./counter.js');
counter.add();
console.log(counter.count);
// counter.js
var Counter = module.exports = {
count: 1,
add: function() {
Counter.count += 10;
},
remove: function() {
Counter.count += 10;
}
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*ber 13
不确定这是否新,但你确实可以在文件之间共享变量:
main.js
exports.main = {
facebook: null
};
Run Code Online (Sandbox Code Playgroud)
counter.js
var jamie = require('./main');
console.info(jamie); //{facebook: null}
jamie.main.facebook = false;
console.info(jamie); //{facebook: false}
Run Code Online (Sandbox Code Playgroud)
anothercheck.js
var jamie = require('./main');
console.info(jamie); //{facebook: null} //values aren't updated when importing from the same file.
jamie.main.facebook = true;
console.info(jamie); //{facebook: true}
Run Code Online (Sandbox Code Playgroud)
现在您可以在文件之间共享.
小智 13
我知道我回答这个问题有点晚了,才7年!
您可以简单地使用全局变量:
global.myVar = 'my-val';
console.log(myVar); // returns 'my-val'
// from here on it's accessable to all modules by just the variable name
Run Code Online (Sandbox Code Playgroud)
小智 7
我和你有同样的问题,.. 有时我想在多个文件之间共享变量,因为我喜欢模块化风格,例如。在 node.js 脚本的不同文件夹/文件中分离控制器、函数、模型,以便我可以轻松管理代码。
我不知道这是否是最好的解决方案,但我希望能满足您的需求。
模型/data.js
// exports empty array
module.exports = [];
Run Code Online (Sandbox Code Playgroud)
控制器/somecontroller.js
var myVar = require('../models/data');
myVar.push({name: 'Alex', age: 20});
console.log(myVar);
// [{ name: 'Alex', age: 20 }]
Run Code Online (Sandbox Code Playgroud)
控制器/anotherController.js
var myVar = require('../models/data');
console.log(myVar);
// This array has value set from somecontroller.js before...
// [{ name: 'Alex', age: 20 }]
// Put new value to array
myVar.push({name: 'John', age: 17});
console.log(myVar);
// Value will be added to an array
// [{ name: 'Alex', age: 20 }, { name: 'John', age: 17}]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52305 次 |
| 最近记录: |