Module. exports in Node. js

Source: Internet
Author: User

Module. exports in Node. js
When writing node. js code, we often need to write modules by ourselves ). At the same time, you also need to write the module interface at the end of the module to declare what content this module exposes to the outside world. In fact, the module interfaces of node. js are written in different ways. Here, the author makes a simple summary. The following code returns a JSON Object, which is a simple example. 1 var exp = {2 "version": "1.0.0", 3 "function1": null, 4 "module1": null, 5}; 6 module. exports = exp; this method can be used to return global shared constants or variables, such as 1 // MATH. js2 var MATH = {3 "pi": 3.14, 4 "e": 2.72, 5}; 6 7 module. exports = MATH; the call method is 1 var MATH = require (". /MATH ") 2 console. log (MATH. pi); this method can also be used to return several other require modules. You can implement multiple require modules at a time. 1 // module_collection.js2 var module_collection = {3 "module1": require (". /module1" ), 4 "module2": require (". /module2 "), 5}; 6 7 module. exports = module_collection; the call method 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 there are also variants in this method, as shown below, several functions are usually returned. js 2 var func1 = function () {3 console. log ("func1"); 4}; 5 6 var func2 = function () {7 Console. log ("func2"); 8}; 9 10 exports. function1 = func1; 11 exports. function2 = func2; the call method is 1 var functions = require (". /functions "); 2 functions. function1 (); 3 functions. function2 (); returns a constructor, that is, a class. The following is a simple example. 1 // CLASS. js 2 var CLASS = function (args) {3 this. args = args; 4} 5 6 CLASS. prototype. func = function () {7 console. log ("CLASS. func "); 8 console. log (this. args); 9}; 10 11 module. exports = CLASS; the call method is 1 var CLASS = require (". /CLASS ") 2 var c = new CLASS (" arguments "); returns a common function as follows: a simple example 1 // func. js2 var func = function () {3 console. log ("this is a testing function"); 4}; 5 6 module. exports = func; The call method is 1 var func = require (". /func "); 2 func (); returns an object. The following is a simple example 1 // CLASS. js 2 var CLASS = function () {3 this. say_hello = "hello"; 4}; 5 6 CLASS. prototype. func = function () {7 console. log ("I say" + this. say_hello); 8}; 9 10 module. exports = new CLASS (); the call method is 1 var obj = require (". /CLASS "); 2 obj. func (); singleton mode sometimes we need the module to return a singleton. the preceding methods 1 and 4 can be used for implementation. That is, the following two forms: 1 // MATH. js2 var MATH = {3 "pi": 3.14, 4 "e": 2.72, 5}; 6 7 module. exports = MATH; and 1 // CLASS. js 2 var CLASS = function () {3 this. say_hello = "hello"; 4}; 5 6 CLASS. prototype. func = function () {7 console. log ("I say" + this. say_hello); 8}; 9 10 module. exports = new CLASS (); finally, I really like JavaScript, which is very convenient. In addition, the emergence of node. js greatly enhances the ability of this language. Look at it!

Related Article

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.