I understand the difference between the exports and the module.exports, welcome everyone to spit Groove ~
In order to better understand exports
and module.exports
the relationship, we first to complement the JS Foundation. Example:
App.js
var a = {name: ' NSWBMW 1 '}; var b == ' nswbmw 2 '; Console.log (a); Console.log (b); var b = {name: ' NSWBMW 3 '};console.log (a); Console.log (b);
The result of running App.js is:
d:\>' NSWBMW 1 "NSWBMW 1" NSWBMW 2"NSWBMW 2"NSWBMW 2 " NSWBMW 3 ' }d:\>
To explain: A is an object, B is a reference to a, that is, A and B point to the same object, i.e. A and B point to the same memory address, so the first two outputs are the same. When B is modified, that is, A and B point to the same memory address of the content has changed, so a will also be reflected, so the 34th output is the same. When B is completely covered, B points to a new memory address (and does not modify the original memory block), a or point to the original memory block, that is, A and b no longer point to the same piece of memory, that is, A and B have no relationship at this time, so the last two output is different.
Having understood the above example, we went to the chase. We only need exports
module.exports
to know the difference between three points to know:
1 exports is a reference to the Module.exports point 2 Module.exports Initial value is an empty object {}, so the exports initial value is also {} 3 require () returns module.exports instead of exports
So:
var name = ' NSWBMW '; = name; function () { console.log (name); }
exports
Assigning a value is actually module.exports
adding two attributes to the empty object, the code above is equivalent to:
var name = ' NSWBMW '; = name; function () { console.log (name); }
We usually use this exports
andmodule.exports
A simple example of calculating the area of a circle:
Using exports
App.js
var circle = require ('./circle '); Console.log (Circle.area (4));
Circle.js
function (r) { return R * R * Math.PI; }
Using Module.exports
App.js
var area = require ('./area '); Console.log (Area (4));
Area.js
function (r) { return R * R * Math.PI; }
The above two examples output is the same. You may ask, why not write like this?
App.js
var area = require ('./area '); Console.log (Area (4));
Area.js
function (r) { return R * R * Math.PI; }
Running the above example will cause an error. This is because, in the previous example, by exports
adding a property, only to the exports
point of memory has been modified, and
function (r) { return R * R * Math.PI; }
In fact, the exports
overlay, that is, exports
point to a new piece of memory (content is a function to calculate the circle area), that is, exports
and module.exports
no longer point to the same piece of memory, that is, there exports
module.exports
is no connection, that is to say module.exports
The block of memory pointed to does not make any changes, still an empty object, that is, {}
area.js exported an empty object, so we call in the App.js area (4) will be reported TypeError: object is not a function
error.
So, one sentence to summarize: When we want the module to export an object, exports
and module.exports
all can be used (but exports
also can not be re-overwritten as a new object), and when we want to export non-object interface, we must also only overwrite module.exports
.
We often see this way of writing:
Exports = Module.exports = somethings
The above code is equivalent to
Module.exports = somethings = Module.exports
The reason is also very simple, module.exports = somethings
is to module.exports
overwrite, at this time module.exports
and exports
the relationship break, point to the module.exports
new memory block, and exports
still point to the original memory block, in order to make module.exports
and exports
still point to the same block of memory or point to the same "Object," so we just exports = module.exports
.
The difference between exports and module.exports