Home > Web Front-end > JS Tutorial > In-depth understanding of Node module

In-depth understanding of Node module

亚连
Release: 2018-05-29 09:18:03
Original
1684 people have browsed it

This article mainly introduces an in-depth understanding of the Node module. Now I share it with you and give you a reference.

When developing complex web applications, it is usually necessary to split each function, encapsulate it into different files, and reference the file when needed, that is, modular management of the code. Almost all programming languages ​​have their own module organization methods, such as packages in Java and assemblies in C#, and Node adopts the CommonJS module specification.

Module specification

CommonJS aims to standardize JS that runs outside the browser and has solved a large number of JS problems ( such as global naming conflicts). In Node's implementation of CommonJS, each module will be encapsulated in a separate JS file, that is, a file is a module, and the file path is the module name. When writing each module, the following three predefined variables are available:

require()

This function is used in the current module To load and use other modules, pass in a module name and return a module export object. The module name can use a relative path (starting with ./) or an absolute path (starting with / or a drive letter such as C:). In addition, the .js extension in the module name can be omitted. At this time, Node will look for a folder with the same name. If it cannot be found, it will look for a js file with the same name. You can also use this function to load and use a JSON file, but the .json extension cannot be omitted.

exports

This object is the export object of the current module. It is used to export the public methods and properties of the module. It defaults to an empty object {}. When other modules use the current module through the require() function, they get the exports object of the current module. A public method is exported in the following code:

exports.hello = function() {
  console.log("Hello World!");
};
Copy after login

module

This object is used to provide metadata and other related information of the current module, but the most useful one is to use it The exports attribute replaces the export object of the current module. For example, the module export object is a normal object by default. You can use the following method to turn it into a function:

module.exports = function() {
  console.log("Hello World!");
};
Copy after login

Note: When using the above method, all modifications to the exports object will be ignored!

Module initialization

The JS code in a module is only executed once when the module is used for the first time, and is initialized during execution. The module's exported object. Later, the cached export object is reused.

Define a module in test.js

//定义私有变量
var name = "";
function setName(n) {
  name = n;
}

function logName() {
  console.log(name);
}

//导出公有方法
exports.setName = setName;
exports.logName = logName;
Copy after login

Load the test module in main

//加载test模块
var test1 = require("./test.js"),
  test2 = require("./test.js");

//使用test1
test1.setName("Neo");

//使用test2
test2.logName(); //Neo
Copy after login

It can be seen that no matter how many times require() is called, the same module is only loaded once , the reference obtained multiple times is actually the same instance.

Main module

The module passed to Node through the command line parameters to start the program is called the main module. The main module is responsible for scheduling and composing the entire application. Other modules of the program work together. For example, when starting the program through the following command line, main.js is the main module:

$ node main.js
Copy after login

Binary module

In addition to Node, you can use JS to write modules , also supports using C/C to write binary modules. The compiled binary modules can be used in the same way as JS modules except that the file extension is .node. Although binary modules can use all functions provided by the operating system, they are difficult to use across platforms.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Summary of the method of converting JS from non-array object to array

js canvas implements the sliding puzzle verification code function

Detailed explanation of red-black tree insertion and examples of Javascript implementation methods

The above is the detailed content of In-depth understanding of Node module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template