I don't understand why only object are nice exported. Look that:
first file
var a = exports.a = {};
var b = exports.b = 0;
exports.xxxxx = function(){
a.hello = 'help';
b = 104;
};
second file
var a = require('firstfile').a;
var b = require('firstfile').b;
module.exports = function(){
console.log(a); // {hello:'help'}
console.log(b); // 0
};
Why ?
and the solution for exports b is do like that:
first file:
...
a.hello = 'help';
exports.b = 104; // (add exports)
...
second file:
...
var b = require('firstfile');
...
console.log(b.b); // 104
Why?
EDIT
I think I understood.
The variable a take just a reference! I will explain to someone who does not understand:
a = {}, b = 0exports.b = 104 // we change the value of the module! who is required.Now, why object are different ?
it is important to remember two things:
Bye! Hope this help!
When you assign 0 to exports.b, it is essentially assigned as a constant value. You can update the value pointed to by the b variable, but it won't be re-exported.
For instance, I've modified what you posted slightly by adding a getB function:
var a = exports.a = {};
var b = exports.b = 0;
exports.xxxxx = function(){
a.hello = 'help';
b = 104;
};
exports.getB = function() { return b; };
Then, you can see what I mean by playing around with this file in REPL:
> var example = require('./example.js');
undefined
> example
{ a: {},
b: 0,
xxxxx: [Function],
getB: [Function] }
> example.xxxxx()
undefined
> example.getB()
104
> example.b
0
>
edit: your comment about references is somewhat correct. When you initially say
var b = exports.b = 0;
You are saying two variables point to the same value. This is different than two variables pointing to the same reference (an object, as you've done with a). When you modify one value, the other value is not modified. In other words, when you change the value of 0 to 1, you don't want every instance of 0 in your code to then also be 1. That wouldn't be good.
If you want b to be a value and to get updated every time you modify that value, you should reassign exports.b. This is part of why you'll see most code as one object being exported... then, your export is treated like a reference and all your b's are updated as expected.