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

查看:119
本文介绍了共享与分享修改多个文件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);

console.log

 1
 11
 -9

背景:

我有一个应用程序(irc bot),我想添加一个功能,使偷窥者可以执行@add 1或@remove1.我有一个main.js,然后根据所说的触发器需要不同的文件.因此,add将触发add.js文件,然后将需要require('main.js')并添加10(为简化起见为10,它将实际上解析该数字并使用该数字).我遇到的问题是有人去做@remove.它需要('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进行了很好的搜索,但没有遇到上面列出的示例. 文档中没有包含任何与我想要做的事情相似的示例;和这些问题 1 2 我了解-但对我没有任何用处-因为我了解正在发生的事情说在那里.

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天全站免登陆