简体   繁体   English

通过node中的module.exports获取变量

[英]Getting a variable via module.exports in node

I have a variable var1 in a class A.js that is a module level variable. 我在类A.js中有一个变量var1 ,它是模块级别的变量。 However, I have some unit tests that check to make sure the contents of var1 are what I expect. 但是,我进行了一些单元测试,以确保var1的内容符合我的期望。 To make it so my test file can access var1 , I used module.exports . 为了使测试文件可以访问var1 ,我使用了module.exports

A.js A.js

var var1 = {};

function resetVar1() {
    var1 = {};
}
module.exports.resetVar1 = resetVar1

function A() {
    // some init stuff
}

A.prototype.addVal = function (key, val) {
    // some code
    var1[key] = val;
    // some more code
}
module.exports.var1 = var1;

My test cases also run some code that update var1 in A.js . 我的测试用例还运行一些代码,以更新A.js var1 When I run this code, it updates A.var1 in my test file the first time, but then all other times, it isn't updated (example shown below) 当我运行此代码时,它将第一次更新测试文件中的A.var1 ,但随后所有其他时间都不会更新(如下所示的示例)

testA.js testA.js

var A = require('A');

test('test1', function (assert) {
    var a = new A();
    a.addVal('key1', 'val1');
    console.log(A.var1); // prints {'key1': 'val1'}
});

test('test1', function (assert) {
    A.resetVar1();
    var a = new A();
    a.addVal('key2', 'val2');
    console.log(A.var1); // still prints {'key1': 'val1'}
});

var1 in A.js behaves as expected. A.js var1的行为符合预期。 My question is why A.var1 is getting updated the first time I call addVal in testA.js , but not any other time? 我的问题是,为什么我第一次在testA.js调用addVal而不是其他任何时间更新A.var1?

When you do this: 执行此操作时:

module.exports.var1 = var1;

You are pointing module.exports.var1 at the same object as var1 . 您将module.exports.var1指向与var1相同的对象。 That is presumably what you want. 那大概就是你想要的。

But, when you then call resetVar1() with this: 但是,当您使用以下命令调用resetVar1()时:

function resetVar1() {
    var1 = {};
}

This assigns var1 a new empty object. 这为var1分配了一个新的空对象。 But, module.exports.var1 still points at the original object so nobody outside of this module will see the new variable. 但是, module.exports.var1仍指向原始对象,因此该模块之外的任何人都看不到新变量。 If you truly want to reset a shared object to be an empty object, but you don't want to break the sharing, then you will need to not assign it a new object, but rather just remove all the properties on the existing object. 如果您确实想将共享库重置为空对象,但又不想中断共享,则无需为它分配新对象,而只需删除现有对象的所有属性即可。

// remove all properties from the var1 object
// so we can clear it, but not break the references to it that others have
function resetVar1() {
    for (var prop in var1) {
         if (var1.hasOwnProperty(prop)) {
             delete var1[prop];
         }
    }
}

module.exports.var1 = var1; sets the value of the locally scoped var1 variable (the initial empty object) to the exported property. 将本地范围内的var1变量(初始空对象)的值设置为exported属性。 When you later set a new value to var1 inside resetVar1 , the exported property still has the original object, so you get the same result. 当您稍后在resetVar1内将新值设置为var1 ,导出的属性仍然具有原始对象,因此您将获得相同的结果。

User jfriend00 is correct, you have to delete each property instead of assigning a new object. 用户jfriend00是正确的,您必须删除每个属性而不是分配新对象。

Another approach is to use a Map , which has a clear method. 另一种方法是使用Map ,它具有clear方法。

var var1 = new Map();

module.exports.var1 = var1;

...

function A() {}

A.prototype.addVal = function(key, val) {
    var1.set(key, val);
}

A.prototype.getVal = function(key) {
    return var1.get(key);
}

...

function resetVar() {
    var1.clear();
}

module.exports.resetVar = resetVar;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM