Analysis of module interface module.exports in node. js

Source: Internet
Author: User

When writing the node. JS Code, we often need to write our own module. At the same time, it is necessary to write the module interface at the end of the module, declaring what the module exposes externally. In fact,node. JS has a number of different syntax interfaces. Here the author makes a brief summary of this.

    1. Returns a JSON Object

      The following code is a simple example.

      1 var exp =2   "version": "1.0.0"3   null4    null,5}; 6 module.exports = exp;

      This method can be used to return some globally shared constants or variables, such as

      1 // Math.js 2 var MATH = {3   "PI": 3.14,4   "E": 2.72,5}; 6 7 module.exports = MATH;

      Call mode is

      1 var MATH = require ("./math")2 console.log (Math.PI);

      This method can also be used to return several require of the other modules, can be implemented once require multiple modules

      1 // Module_collection.js 2 var module_collection = {3   "Module1": Require ("./module1"),4   "Module2": Require ("./module2"),5}; 6 7 module.exports = module_collection;

      Call mode is

      1 var module_collection = require ("./module_collection"); 2 var module1 = module_collection.module1; 3 var module2 = module_collection.module2; 4 // Do something with Module1 and Module2

      In fact, there is a variant of this approach, such as the following, usually can return several functions

      1 //Functions.js2 varFunc1 =function() {3Console.log ("Func1");4 };5 6 varFunc2 =function() {7Console.log ("Func2");8 };9 TenExports.function1 =func1; OneExports.function2 = Func2;

      Call mode is

      1 var functions = require ("./functions"); 2 Functions.function1 (); 3 functions.function2 ();

    2. Returns a constructor, which is a class

      The following is a simple example.

      1 //Class.js2 varCLASS =function(args) {3    This. args =args;4 }5 6CLASS.prototype.func =function() {7Console.log ("Class.func");8Console.log ( This. args);9 };Ten  OneModule.exports = CLASS;

      Call method to

      1 var class = require ("./class")2varnew CLASS ("arguments");

    3. Returns a normal function

      The following is a simple example

      1 // Func.js 2 var function () {3   Console.log ("This is a testing function"); 4 }; 5 6 module.exports = func;

      Call method to

      1 var func = require ("./func"); 2 func ();

    4. Returns an object

      The following is a simple example

      1 //Class.js2 varCLASS =function() {3    This. Say_hello = "Hello";4 };5 6CLASS.prototype.func =function() {7Console.log ("I say" + This. Say_hello);8 };9 TenModule.exports =NewCLASS ();

      Call method to

      1 var obj = require ("./class"); 2 obj.func ();

Single-Case mode

Sometimes we need the module to return a singleton singleton. Can be implemented using the above Method 1 and 4来. Which is the following two forms

1 // Math.js 2 var MATH = {3   "PI": 3.14,4   "E": 2.72,5}; 6 7 module.exports = MATH;

And

1 //Class.js2 varCLASS =function() {3    This. Say_hello = "Hello";4 };5 6CLASS.prototype.func =function() {7Console.log ("I say" + This. Say_hello);8 };9 TenModule.exports =NewCLASS ();

Finally, I really like the language of JavaScript, very convenient. And the advent of node. JS greatly enhanced the language's ability. Watch it!

Analysis of module interface module.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.