Exports and Model.exports in node. js

Source: Internet
Author: User

Nodejs compile JS file to the JS file packaging

Nodejs Compile the JS file will be the content of the JS file to the end of the packaging (function (exports,require,module,__filename,__dirname) {\ n and \}), the normal JS file becomes:

(function (exports,require,module,__filename,__dirname) {

var a=function () {};

..............

})

After packing, we will have variable exports,require,module in every JS file. The packaging prevents global contamination

What is the difference between exports and module.exports?

After wrapping the contents of the JS file, a function is returned, and in the arguments passed when the function executes, exports and module.exports point to the same variable that can be verified in the following way:

Save the following code to the Nodejsbuild.js file

Console.log (arguments)

Console.log (Exports==exports)

Run Node Nodejsbuild.js

The exports and model.exports that are passed in initially are equal when we assign operations to exports or Models.exports to assign operations, the relationship between the two is destroyed, and the last use is actually model.exports. When I used it, I found that there was a problem.

Through the above if you can clearly know what happened, and left after the result is not the same as expected. When we assign a value to exports or model.exports, we need to assign a value to ensure consistency, otherwise the last use may not be the same as expected.

It is common to assign values to a variable that causes the referenced object to change the result of the last program run and the expected inconsistency.

Exports and Model.exports in node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.