node. JS: module

Source: Internet
Author: User

Summary : This blog mainly introduces node. JS Modules

1. Create a module

Creating a module in node. js is simple, because a file is a module. We just need to understand how to get this module from other files. node. JS provides exports and require two objects, where exports is the public interface of the module, and require is used to obtain an interface from the external module, the exports object of the acquired module. Look at the following example.

Create a Module.js file with the following content:

varfunction(thyname) {    =function() {    Console.log ( ' Hello ' +name);}

Create a getmodule.js in the same level directory with the following content:

var mymodule = require ('./module '); Mymodule.setname (' byvoid '); Mymodule.sayhello ();

Run the getmodule.js and get the result: Hello byvoid.

You may already be able to understand the difference between exports and require objects. Module.js through the Exports object SetName and SayHello as the interface of the module, in Getmodule.js through the Require ('./module ') load this module, You can then directly access the members of the exports object in Module.js.

2. One-time loading

Require does not load the module repeatedly, and the result of the final output is determined by the latter, just like the same definition of CSS properties, which overrides the former.

For example, we make a slight modification on the basis of Getmodule.js:

var hello1 = require ('./module '); Hello1.setname (' byvoid 1 '); var hello2 = require ('./module '); Hello2.setname (' byvoid 2 '); Hello1.sayhello ();

The result of the operation is: Hello Byvoid 2. This is because the variables hello1 and Hello2 point to the same instance, so the hello1.setname result is hello2.setname overwritten.

3. Cover exports

Sometimes we just encapsulate an object into a module, for example:

function Hello () {    var  name;      This function (thyname) {        = thyname;    };      This function () {        console.log (' Hello ' + name);     = Hello;

At this point we need to pass require ('./singleobject ') in other files. Hello to get the Hello object, which is slightly redundant, so you can use the following method to simplify it slightly:

function Hello () {     var  name;       This function (thyname) {         = thyname;     };       This function () {         console.log (' Hello ' + name);      = Hello;

This allows you to get the object directly:

var Hello = require ('./hello 'new  hello (); Hello.setname (' byvoid '); Hello.sayhello ();

  Note : The only change to the module interface is to use Module.exports = Hello instead of Exports.hello = hello. When the module is referenced externally, its interface object is the Hello object itself to be exported, not the original exports.

  warning : It is not possible to assign a value to a module.exports by exports Direct assignment . Exports is actually just a variable that points to the same object as the Module.exports, which itself is released after the module execution ends, but module does not, so you can only change the provider by specifying Module.exports.

node. JS: module

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.