English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
require is used to load code, while exports and module.exports are used to export code. But many beginners may be confused about the difference between exports and module.exports, in order to better understand the relationship between exports and module.exports, let's first consolidate the basics of js. Example:
app.js
var a = {name: 'nswbmw 1} var b = a; console.log(a); console.log(b); b.name = '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:
{ name: 'nswbmw 1} { name: 'nswbmw 1} { name: 'nswbmw 2} { name: 'nswbmw 2} { name: 'nswbmw 2} { name: 'nswbmw 3}
Let me explain:a is an object, b is a reference to a, that is, a and b point to the same object, that is, a and b point to the same memory address, so the first two outputs are the same. When modifying b, that is, the content of the same memory address pointed to by a and b is changed, so a will also reflect this, so the third and fourth outputs are the same. When b is completely overwritten, b points to a new memory address (the original memory block is not modified), and a still points to the original memory block, that is, a and b no longer point to the same memory, which means that at this time, a and b have no relationship at all, so the last two outputs are different.
After understanding the above examples, let's get to the main topic.
We only need to know three points to understand the difference between exports and module.exports:
So: we use
var name = 'nswbmw'; exports.name = name; exports.sayName = function() { console.log(name); }
Assigning a value to exports is actually adding two properties to the empty object module.exports, the above code is equivalent to:
var name = 'nswbmw'; module.exports.name = name; module.exports.sayName = function() { console.log(name); }
We usually use exports and module.exports in this way
A simple example to calculate the area of a circle:
Using exports
app.js
var circle = require('./circle'); console.log(circle.area(4));
circle.js
exports.area = function(r) { return r * r * Math.PI; }
Using module.exports
app.js
var area = require('./area'); console.log(area(4));
area.js
module.exports = function(r) { return r * r * Math.PI; }
The outputs of the two examples above are the same. You may wonder, why not write it like this?
app.js
var area = require('./area'); console.log(area(4));
area.js
exports = function(r) { return r * r * Math.PI; }
Running the above example will result in an error. This is because, in the previous example, adding properties to exports only modified the memory pointed to by exports, and
exports = function(r) { return r * r * Math.PI; }
In fact, it is an overwrite of exports, which means exports points to a new memory block (containing a function to calculate the area of a circle), that is to say, exports and module.exports no longer point to the same memory block, that is to say, at this time, exports and module.exports have no connection, that is to say, the memory block pointed to by module.exports has not been changed at all, it is still an empty object {} , that is to say, area.js exported an empty object, so when we call area in app.js,4An error of TypeError: object is not a function will be reported.
So, in a word, to summarize: when we want to export an object from the module, both exports and module.exports can be used (but exports cannot be overwritten as a new object), and when we want to export a non-object interface, we must and can only overwrite module.exports.
We often see such writing:
exports = module.exports = somethings;
The above code is equivalent to
module.exports = somethings; exports = module.exports;
The reason is also very simple, module.exports = somethings is a cover of module.exports, at this time, the relationship between module.exports and exports is broken, module.exports points to a new memory block, and exports still points to the original memory block. In order to make module.exports and exports still point to the same memory block or point to the same “object”, so we exports = module.exports.
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the呐喊 tutorial!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.