Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - changing the value of an exported integer

I have a file A.js where I have a module-level variable activeCount. I used module.exports to export it. I have a test file testA.js where I check the value of activeCount.

However it seems that the changes I make in A.js to activeCount aren't seen by testA.js. I think this is probably because when I change activeCount, it causes module.exports.activeCount and activeCount to point at different objects. Am I analyzing this correctly, and if so how do I change the value of activeCount without creating a new object?

A.js

var activeCount = 0;
...

function reset() {
    activeCount = 0;
}

function A() {
    ...
}
module.exports = A;
module.exports.activeCount = activeCount;
module.exports.reset = reset;

A.prototype.addFunction(...) {
    ...
    activeCount++;
    ...
}

testA.js

var A = require('A');

test('test1', function (assert) {
    var a = new A();
    a.addFunction(...);
    console.log(A.activeCount); // prints 0 instead of 1
});

test('test2', function (assert) {
    A.reset();
    var a = new A();
    a.addFunction(...);
    console.log(A.activeCount); // also prints 0 instead of 1
});
like image 271
user1998511 Avatar asked Sep 18 '15 23:09

user1998511


People also ask

How do you assign a value to a variable in node JS?

The default value of variables that do not have any value is undefined. You can assign a value to a variable using the = operator when you declare it or after the declaration and before accessing it. In the above example, the msg variable is declared first and then assigned a string value in the next statement.

Can I export variables in JS?

Use named exports to export multiple variables in JavaScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables can be imported by using a named import as import {A, B} from './another-file.

How do I export multiple values in node JS?

To export multiple functions in JavaScript, use the export statement and export the functions as an object. Alternatively, you can use the export statement in front of the function definitions. This exports the function in question automatically and you do not need to use the export statement separately.


1 Answers

In Javascript, when you assign an integer (or any primitive - e.g. non-object), it is copied into the new variable. Thus a change to the new variable does not influence the original variable. Javascript does not have a true reference type for integers.

The usual work-around is to export an object (which is assigned by pointer) and then put the integer as a property on that object. Then, changing the property on the object will be seen by everyone who has a reference to that object.

As it turns out, you are already exporting activeCount as a property of an object (since it is a property of module.exports in the A.js module. So, you just need to change the "A" module to use it from there rather than use it's local copy. There are a couple different ways to do that. Here's one:

// A.js
function reset() {
    // change the exported variable
    module.exports.activeCount = 0;
}

function A() {
    ...
}
module.exports = A;
module.exports.activeCount = 0;
module.exports.reset = reset;

A.prototype.addFunction(...) {
    ...
    // change the exported variable
    module.exports.activeCount++;
    ...
}

Now, your testA.js module will work as expected.


Note, since you are doing module.exports = A; and functions are objects, you could also reference activeCount as a property of A while inside the A module and it would also solve the problem and give the desired result:

// A.js
function reset() {
    // change the exported variable
    A.activeCount = 0;
}

function A() {
    ...
}

// add properties to our constructor function so those properties
// are also exported
A.activeCount = 0;
A.reset = reset;
module.exports = A;

A.prototype.addFunction(...) {
    ...
    // change the exported variable
    module.exports.activeCount++;
    ...
}
like image 95
jfriend00 Avatar answered Sep 22 '22 14:09

jfriend00