zhiqingstudy

Be a young person with knowledge and content

Modularization refers to the process of dividing the system into several modules layer by layer from top to bottom when solving a complex problem. For the whole system, modules are units that can be combined, decomposed and replaced.

Modularization in the field of programming is to follow fixed rules and break a large file into independent and interdependent modules.

Advantages of modular code splitting:

1. Improve code reusability

2. Improve code maintainability

3. It can be loaded on demand

Everyone writes code according to the same modular specification, which reduces the communication cost and greatly facilitates the mutual call between modules.

In Node.js, according to the source of modules, modules are divided into three categories:

·Built in module (the built-in module is officially provided by Node.js, such as fs, path, http, etc.)

·Custom module (each. js file created by the user is a custom module)

·Third party module (the module developed by a third party is not an official built-in module, nor a module created by the user, and needs to be downloaded before use)

What is module scope?

Similar to function scope, variables, methods, and other members defined in a custom module can only be accessed within the current module. This module level access restriction is called module scope.

Benefits of module scope

Prevent global variable pollution

How to share members in the scope of a module outward?

In each. js user-defined module, there is a module object, which stores information related to the current module. The module object contains an exports object, which can share members externally through module.exports.

When importing custom modules with the require method, the object referred to in module.exports is obtained

const m=require('./自定义模块') // 导入自定义模块
console.log(m) // {}  在一个自定义模块中,默认情况下,module.exports={}

Custom file module code

module.exports={
    // 向module.exports对象挂载name属性
    'name':'node.js',
    // 向module.exports对象挂载sayHello方法
    'sayHello':function(){
        console.log('hello node.js')
    }
}

Exports object

Because the word module.exports is complex to write, Node provides an exports object in order to simplify the code of outward sharing members. By default, exports and model.exports point to the same object, and the final sharing result is subject to module.exports

Modular specification in Node.js

Node.js follows the CommonJS modularization specification, which specifies the characteristics of modules and how they depend on each other.

CommonJS regulations:

1. Inside each module, the module variable represents the current module.

2. The module variable is an object whose exports attribute (i.e. module. exports) is an external interface.

3. Loading a module is actually loading the module.exports attribute of the module. The require() method is used to load the module

comment
head sculpture
Code:
Related

The art of interpersonal communication and communication

A code of conduct that regulates and regulates interpersonal relationships

The importance of interpersonal relationships




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号