Original:http://www.hacksparrow.com/node-js-exports-vs-module-exports.html
Reference:http://www.cnblogs.com/kongxianghai/p/3936197.html
1. exports object in the Nodejs module: You can use it to create your module. For example:
Suppose this is a rocker.js file:
# Rocker.js
Exports.name = function () { console.log (' My name is Lemmy Kilmister ');};
Then, in another file, you can refer to this:
var rocker = require ('./rocker.js ');
Rocker.name (); ' My name is Lemmy Kilmister '
Emphasize:
(1) Assigning a value to a exports is actually adding properties to the empty object of module.exports, eg:
var name = ' NSWBMW '; Exports.name = name; Exports.sayname = function () {
Module.exports.sayName = function () { console.log (name);}
(2) The use of the two different methods, eg:
Using exports:
# Circle.js--Define a method with exports
Exports.area = function (r) {
# App.js--using the method defined in the above file
Console.log (Circle.area (4)); # Use, reference way : ***.area ()
Using Module.exports:
# Area.js--Define method using Module.exports
Module.exports = function (r) { return R * R * MATH.PI;
Console.log (Area (4)); # Direct Use , * * * ()
2. Module.exports :
- Module.exports is the real interface, exports is just one of its auxiliary tools.
- The final return to the call is module.exports instead of exports.
- All the properties and methods collected by the exports are assigned to Module.exports. Of course, there is a premise that the module.exports itself does not have any attributes or methods. If Module.exports already has some properties and methods, the information collected by exports will be ignored. Eg: Modify Rocker.js as follows:
# Modified Rocker.js
' ROCK It! ' ; = function () { console.log ('My name is Lemmy Kilmister'
At this point, the execution rocker.js is referenced again as follows:
var rocker = require ('./rocker.js ');
//
# Found Error: Object "ROCK it!" No Name method
The rocker module ignores the name method collected by exports and returns a string "ROCK it!". Your module does not necessarily have to return "instanced objects". Your module can be any legitimate JavaScript object--boolean, number, date, JSON, string, function, array, and so on.
- Your module can be anything you set to it. If you do not explicitly
Module.exports
set any properties and methods, then your module is exports set toModule.exports
的
属性。
eg1: 你的模块是一个类:
# Modify Rocker.js again
Module.exports = function (name, age) { this.name = name; This.age = age; This.about = function () { console.log (this.name + ' is ' + this.age + ' years old ');}; };
You can use it this way:
var rocker = require ('./rocker.js ');
var r = new Rocker (' Ozzy ', 62);
R.about (); Ozzy is years old
eg2: 你的模块是一个数组:
# Modify Rocker.js again
Module.exports = [' Lemmy Kilmister ', ' Ozzy Osbourne ', ' Ronnie James Dio ', ' Steven Tyler ', ' Mick Jagger '];
You can use it this way:
var rocker = require ('./rocker.js ');
Console.log (' Rockin in Heaven: ' + rocker[2]); Rockin in Heaven:ronnie James Dio
Now you understand that if you want your module to be a specific type to use Module.exports,直接 变量/new方法 使用
. If you want the module to be a typical "instanced object" , use exports to reference the. * * *.
- Adding a property to Module.exports is similar to adding a property to exports. For example:
Module.exports.name = function () { console.log ('My name is Lemmy Kilmister' );};
Again, exports is like this:
Exports.name = function () { console.log ('My name is Lemmy Kilmister' );};
Emphasis: These two definitions do not work the same (I am still a little dizzy, follow-up will continue to learn). As mentioned earlier, Module.exports is a true interface, and exports is nothing more than an auxiliary tool. It is recommended to export using exports unless you intend to change from the original "Instanced object" to a type!!
NodeJS Exports–exports vs Module.exports