exports of modules and module.exports node.js

Before use module is further found to be not assigned directly by Exports, discloses a variable or a function. In fact, exports like module.exports reference variable, while module.exports direct equivalent of heap memory address, so, at the very beginning, exports are directed module.exports, that exports = module.exports = {} and, by default returns module.exports.

  • Verify module.exports default return 

Create a js file a.js, reads as follows:

var key = 100;
exports =key;
module.exports=key;
return exports;

Print results are as follows: in the implementation of exports = key; when, that is, you assign a value to exports to print out key is empty, however, in the implementation of module.exports = key; when printed out is the value of key

  • Verify the relationship between exports and module.exports  

1. When you give exports, assign variables or functions, module.exports can take to variables and functions added in exports

var key = 100;
exports.key =key;
///module.exports=key;
return exports;

Print out the results are as follows: to show that you are doing an assignment to exports when, module.exports can get to operational content of the exports.

2. In doing assignment on exports and module.exports, exports of assignment is invalid.

var key = 100;
exports.key =key;


function testFunc (argument) {
console.log(argument);	
};
module.exports = {
  testFunc:testFunc
};
return exports;

Print is as follows: find when I give module.exports assignment, exports are not reflected in the assignment module.exports in.

Guess you like

Origin blog.csdn.net/ioszhanghui/article/details/90315197