分享 &修改多个文件 node.js 之间的变量 [英] Sharing & modifying a variable between multiple files node.js

查看:22
本文介绍了分享 &修改多个文件 node.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;

total.js

var count = require('./main.js').count;
console.log(count);

add.js

var count = require('./main.js').count;
count += 10;
console.log(count);

remove.js

var count = require('./main.js').count;
count -= 10;
console.log(count);

控制台.log

 1
 11
 -9

背景:

我有一个应用程序(irc bot),我想添加一个功能,peeps 可以执行 @add 1 或 @remove 1.我有一个 main.js,然后根据所说的触发器需要不同的文件.所以 add 将触发 add.js 文件,然后将 require('main.js') 并添加 10(为简化为 10,它实际上会解析数字并使用该数字).我遇到的问题是当有人走动并执行@remove 时.它 require('main.js') 并从 1 中减去 10 结果为 -9.并且执行@total 将输出 1.

Background:

I have an application (irc bot), and I want to add a feature that peeps can do @add 1 or @remove 1. I have a main.js that then requires different files depending on the triggers that are said. So add would trigger the add.js file, and that would then require('main.js') and add 10 (10 for simplification, it'll actually parse the number and use that number) to it. The problem I'm having is when someone goes about and does @remove. It require('main.js') and subtracts 10 from 1 resulting in -9. And doing @total would output 1.

我已经对 module.exports 进行了相当好的搜索,但没有遇到过我上面列出的示例.文档 不包含任何与我想要做的事情接近的示例;而这些问题12 我理解——但对我没有任何用处——因为我理解那里说.

I've done a fairly good search for module.exports and I haven't come across an example like the one i listed above. The docs don't include any examples close to what I'm wanting to do; and these questions 1, 2 I understand--but aren't of any usefulness to me--as I understand what's being said there.

我想让@add 和@remove 操作同一个变量(count),并且@total 返回总计数并考虑@add 和@removes.我是否错误地使用了 module.exports;或者有没有一种共享变量的通用方式,一个文件可以修改module.exports的内容并将结果返回到main.js文件?

I'd like to have both @add and @remove manipulate the same variable ( count ), and for @total to return the total of count with the @add and @removes taken into account. Am I using module.exports incorrectly; or is there a common way that variables are shared, with one file being able to modify the contents of the module.exports and returning the results to the main.js file?

推荐答案

你的问题是当你做 var count = require('./main.js').count; 时,你得到该号码的副本,而不是参考.改变 count 不会改变源".

Your problem is that when you do var count = require('./main.js').count;, you get a copy of that number, not a reference. Changing count does not change the "source".

但是,您应该具有文件导出功能.需要一个文件只会在第一次运行它,但之后它会被缓存并且不会重新运行.查看文档

However, you should have the files export functions. Requiring a file will only run it the first time, but after that it's cached and does not re-run. see docs

建议#1:

// main.js
var count = 1;
var add = require('./add.js');
count = add(count);

// add.js
module.exports = function add(count) {
    return count+10;
}

#2:

var count = 1;
var add = function() {
    count += 10;
}
add();

#3:我个人会创建一个计数器模块(这是一个单一实例,但您可以轻松地将其设为类"):

#3: Personally i would create a counter module (this is a single instance, but you can easily make it a "class"):

// 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;
    }
}

这篇关于分享 &修改多个文件 node.js 之间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆