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
});
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.
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.
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.
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++;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With