Module, module. Exports, exports

Module object: In each.js custom module there is a Module object that stores information about the current module

Module. exports object: In node custom modules, you can use module.exports objects to share parts of the custom module. When an external (other JS file) imports a custom module using the require() method, it gets the object pointed to by module.exports

Exports object: Node provides exports objects to simplify the code for sharing members outward because the word module.exports is complicated to write.

Exports and module.exports refer to the same object by default

Module. Exports: module. Exports: module. Exports: module. Exports: module

Case 2.

Use cases to understand the relationship between the two

Case 1:

Custom code

Name =" exports. exports "module.exports ={gender :" male ", age:18}Copy the code

Output code

// To test module.exports, Exports /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0Copy the code

The output

Case 1 Explanation: Exports and module.exports refer to the same object by default. Exports and module.exports refer to an empty object. {gender: “male”,age:18}, and passes a reference to module.exports. Module. exports refers to the new object, not to the empty object. Module.exports refers to the object, so the case output is shown above

Case 2

Custom code

Module.exports. Name =" exports "={exports :" male ", age:18}Copy the code

The output

Case 2 Explanation: Module. exports: name=” little wu “, exports: new object {gender: “male”,age:18}, exports: new object {gender: “male”,age:18

Case 3

Custom code

Name =" exports.name "module. Exports. age=18Copy the code

The output

Case 3 Explanation: Similarly, they both start pointing to empty objects. The first line of code mounts a name attribute through the exports reference, and the second line mounts an age attribute through the module.exports reference. There are no new objects here. Module.exports points to this object, so the final output looks like the one above

Four cases

Custom code

Exports ={gender :" male ", name:" small ",} module.exports=exports// set export to module.exports module.exports.age=18Copy the code

The output

Case 4 Explanation: Similarly, they start pointing to the same empty object, execute code, define a new object in memory, and mount the new object on exports, which points to the newly defined object instead of pointing to the previously empty object. Module. exports=exports assigns the export reference to module.exports by equal sign, and module.exports refers to a new object instead of the empty one. Module. exports adds an age property to the new object that it points to, so all of its properties are pointed to by module.exports, so the output looks like this

Important note: It is not recommended to use exports and module.exports in the same module to prevent confusion

From my CSDN article! CSDN also has a lot of dry goods, you can pay attention to oh, regular update knowledge

The original link: blog.csdn.net/xiaowuwjw/a…