function Module(id, parent) { this.id = id; this.exports = {}; this.parent = parent; if (parent && parent.children) { parent.children.push(this); } this.filename = null; this.loaded = false; this.children = []; }
index.js
html
console.log(module)
Module { id: '.', exports: {}, parent: null, filename: '/home/joes/Demo/tests/index.js', loaded: false, children: [], paths: [ '/home/joes/Demo/tests/node_modules', '/home/joes/Demo/node_modules', '/home/joes/node_modules', '/home/node_modules', '/node_modules' ] }
file1.js
node
module.exports = function fn() {};
index.js
函數
var file1 = require('./file1.js') console.log(module)
Module { id: '.', exports: {}, parent: null, filename: '/home/joes/Demo/tests/index.js', loaded: false, children: [ Module { id: '/home/joes/Demo/tests/file1.js', exports: [Function], parent: [Circular], filename: '/home/joes/Demo/tests/file1.js', loaded: true, children: [], paths: [Object] } ], paths: [ '/home/joes/Demo/tests/node_modules', '/home/joes/Demo/node_modules', '/home/joes/node_modules', '/home/node_modules', '/node_modules' ] }
咱們時常會在模塊源碼中看到ui
exports = module.exports = {};
此時兩者指向同一個對象。若是export.a = 1;
此時this
exports = module.exports = {a: 1}
若是使用exports = function() {};
並無導出module,exports
指向了新的對象,module.exports仍是指向原始的對象。又由於module.exports
對象是要傳給require
函數的,因此以後的改變並不會實際導出到外面。spa
http://www.hacksparrow.com/node-js-exports-vs-module-exports.html
http://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-node-jscode