简体   繁体   中英

Node.js : module exports, only object

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:

  • when the first file is read, a = {}, b = 0
  • when we change b, b as been changed, but NOT the module (the exports) !!
  • this is normal that with exports.b = 104 // we change the value of the module! who is required.

Now, why object are different ?

  • because when the file 2 is executed. (executed once), the value of a = {}.
  • but it displays {xxx:xxx}
  • this is because the value of a when the file is executed is not {}, but a reference of this object.
  • So, when we need to take this object, we search all reference.

it is important to remember two things:

  • modules required out of a function are read once! Need to require in the function for see changes.
  • objects stock reference.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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