Home  >  Article  >  Web Front-end  >  Detailed explanation of using Node module module (with code)

Detailed explanation of using Node module module (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-05-03 11:21:203055browse

This time I will bring you a detailed explanation of the use of the Node module module (with code), what are the precautions when using the Node module module, the following is a practical case, let's take a look.

When developing complex web applications, it is usually necessary to split and encapsulate each function into different files and reference the file when needed, that is, to perform modularization management of the code . Almost all programming languages have their own module organization methods, such as packages in Java and assemblies in C#, while Node uses 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 Used to load and use other modules in the current module, passing in a module name and returning 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!");
};

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!");
};

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;

Load the test module in main

//加载test模块
var test1 = require("./test.js"),
  test2 = require("./test.js");
//使用test1
test1.setName("Neo");
//使用test2
test2.logName(); //Neo

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

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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use js after getting ModelAndView

Use v-if and v-show in vue and the difference

The above is the detailed content of Detailed explanation of using Node module module (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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