node. JS Module Definition Summary

Source: Internet
Author: User
Tags what interface

node. JS provides a simple modular system to allow node. js files to be called each other. Modules are a basic part of the node. JS application, and the files and modules are one by one corresponding. In other words, a node. js file is a module that may be JavaScript code, JSON, or compiled C/s extensions.

node. JS provides exports and require two objects, where exports is the interface that the module exposes, and require is used to obtain an interface from the outside of a module, the exports object of the acquired module.

What interface a module needs to expose externally is determined at the time of definition.

When the module is created, we can use the exports and module.exports two methods to expose the interface. Most of the time, the use of the two is equivalent, but there are subtle differences. The difference is as follows:

    1. exportsis a reference to the point module.exports ;
    2. module.exportsThe initial value is an empty object {} , so the exports initial value is also{};
    3. require()is returned module.exports instead ofexports。

Because exports it is a pointer to a module.exports reference, so when we want the module to export an object, exports and module.exports both can be used (but exports also cannot be re-overwritten as a new object), and when we want to export the non-object interface, we must also only overwrite module.exports.

There are several different ways to define the module definition for node, which is summarized as follows:

Returning a JSON object with the following code is a simple example. Exp.js:
var exp = {     "name": "Jeri",     "age": +,     "email": "[email protected]"  = exp;
The method is called: app.js:
var exp = require ("./exp"); Console.log (exp.name);

This method can be used to return some globally shared constants or variables.

For a module, expose several methods. As follows, you can usually return several functions: Exp.js:
var function () {    console.log ("func1");}; var function () {    console.log ("Func2"== Func2;
The method is called: app.js:
var functions = require ("./exp"); Functions.function1 (); Functions.function2 ();
Returns a constructor, a class example, as follows: Exp.js.js
var function (args) {    this.args =function() {    console.log ("Myclass.func" );    Console.log (this =  MyClass;
Call method is App.js
var MyClass = require ("./myclass"); var New MyClass ("arguments");
Returning a normal function as follows is a simple example. exp.js
var function () {    Console.log ("This is a testing function"= func;

The calling method. App.js
var func = require ("./exp"); func ();
Returns an object as follows is a simple example exp.js
var function () {    this. SayHello = 'hello 'function() {    Console.log (this new MyClass ();  
The calling method. App.js
var obj = require ("./myclass"); Obj.func ();
Sometimes we need the module to return a singleton singleton. Can be implemented using the above Method 1 and 4来. That is, the following two kinds of forms: app.js
var Math = {    "PI": 3.14,    "E": 2.72= MATH;
And
var function () {    this. SayHello = "Hello"function() {    Console.log ( this new CLASS (); 
The above is a summary of several ways to define the module.

node. JS Module Definition Summary

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.