Module.export and Export

Source: Internet
Author: User

    • Module.exports and exports are the same objects referenced to, similar to the following code (for example, not completely correct):

var module.exports = {};
var exports = Module.exports;
Within the module, they point to the same empty object before any changes are made to module.exports and exports.

    • The use of require in other modules refers to the Module.exports object, which is not necessarily the reference object that the exports points to, which can easily be caused by ignoring the bug in development. Here's an example:

Exports = function () {};
Within the other modules
Require (...); /get {}
When using require in other modules to refer to an object exported by the above module, it is found to be an empty object, why?       Because Exports=function () {} is used, the reference to exports is pointed to the function, and the Module.exports reference is an empty object, and its His module uses require to refer to module.exports, so there is an empty object, not a function. Of course this is written module.exports = function () {} is definitely not a problem.

    • Some people say that in order to prevent the above bug to occur, it is generally performed under exports = Module.exports, but the direct overwrite module.exports is not a good habit, so for module.exports = {a:123, b:456} Such a situation, separate to write:

EXPORTS.A = 123;
exports.b = 456;

    • Here are a few cases where other modules get different results:

Exports = function fn () {}; Outputs "@routes {}"
Exports.fn = function fn () {}; Outputs "@routes {fn: [FUNCTION:FN]}"
Module.exports = function fn () {}; Outputs "@routes function fn () {}"
Module.exports.fn = function fn () {}; Outputs "@routes {fn: [FUNCTION:FN]}"

Module.export and Export

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.