Recently in the "Node development actual combat detailed" when there are questions, so they have collated some information. Here is the official API document for node4.* (http://nodejs.cn/doc/node_4/modules.html#modules_module_exports), I'm a bit confused, pull out node.10* The official API (Https://nodejs.org/dist/v0.10.9/docs/api/modules.html#modules_module_exports).
Introduction of Module.exports and exports
Both module.exports and exports are called when the function or method is exposed, require, but 2 of them are different. Here's the code:
Ex.js
exports='danhuangmode';
Mex.js
module.exports= ' Danhuangmode ';
Call_ex_mex.js
var ex=require ('./ex ');
var mex=require ('./mex ');
Console.log (ex);
Console.log (' \ n ');
Console.log (MEX);
Execution Result:
The reference exports provides a method, the output is an object, a reference Module.exports provides a method, and the output is a string.
What is the relationship between an external reference and an exports internal interface?
Exports internal processing exposure method, is how to deal with, see the following code:
var string= ' This was in Exports.js 'function ex_fn () { console.log (' the ' this ' Funtion ex_fn ');} var exobj={ str1:"str1 exobj", function () { Console.log ("in function");} ; exports.string=string;exports.ex_fn=ex_fn;exports.exobj=exobj;exports= Exobj;
Calling code:
var ex=require ('./ex '); Console.log (ex.string); Console.log (EX.EX_FN); Console.log (ex.exobj); Console.log (ex);
The results show:
All the interfaces provided by exports directly invoke the exported instantiated interface object, displaying all the provided types, functions, objects, and methods and objects in the interface object.
Module.exports how to deal with external interface?
Mex.js
function () { Console.log (' This in funtion ex_fn ');} module.exports=ex_fn;
Call Code Mex.js:
Reference Mex.js
var ex=require ('./mex ');
EX ();
Console.log (ex);
The result of the execution is:
Directly returns the function as.
Let's look at one more example:
var person={ name:"person's name", age :, function () { returnthis. Age; = person;
Code to invoke:
var person=require ('./modulex '); Console.log (person.name); Console.log (person.age); Console.log ( Person.getage ()); Console.log (person) ;
The results shown are:
Returns a JSON object that can be called directly inside a function, property.
What is the relationship between module.exports and exports?
Module.exports = ' personname '; exports.name=function () { Console.log (' This is Person name ');}
Script to invoke:
var person=require ('./person '); Console.log (person); Console.log (Person.name) ;
Execution Result:
PersonName
Undefined
Results:
In fact, the real interface is Module.exports,exports is an auxiliary tool. The final return is module.exports, not exports.
When Module.exports does not have any properties and methods, exports will pass all the information collected to Module.exports, and if Module.exports already has properties and methods, the information collected by exports will be ignored.
The use and difference of exports and module.export in node