The content of this article is an introduction to the content of the Node.js custom module (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Export
Method 1:
exports .Attribute name = value/variable;
Note: Multiple exports can be exported.Attribute name = value/variable;
Main module/Introduction module.js
const myModule = require("./自定义模块.js"); console.log(myModule.username); console.log(myModule.userage); myModule.getInfo(); console.log(myModule.address);
Self Define module.js
// 自定义模块 // =========1.变量============ let username = "Jack"; let userInfo = {age:10, grade:"H5"}; // 导出/ 暴露内容 exports.username = username; exports.userObj = userInfo; //直接赋值 // =========2.变量============ exports.userage = 18; // =========3.方法============ exports.getInfo = function () { console.log(userInfo,username); } //另一个方法表示 module.exports.address = "北京"; //module.exports 等同于exports console.log(module.exports === exports);//输出true
Method 2:
module.exports = object;
moudule.exports = class/ Constructor/function;
Note: Writing multiple module.exports = objects will overwrite the previous one and modify its value;
The exported class/constructor must be exported through new , the object cannot be new;
Example:
Custom module 2-2.js
//自定义模块2 module.exports = { user:"丽丽", tag: 100 } //方法 //此时user和tag输出的为undefined,因为这个exports方法会直接覆盖上面的exports module.exports = function () { console.log("自定义模块2");//主模块调用:myModule2()或new myModule2() } // 导出===类(构造函数) module.exports = class UserName{ // console.log("我是个类/构造函数");//导进的模块必须通过new 下进行导出,对象不能new } //这样不能进行导出,相当于又声明了一个exports,exports添加属性和方法是可以进行导出,修改属性或者方法就不能进行导出 // exports = { // a:100 // }
Main module.js
// 自定义模块2 console.log("=============自定义模块2==============="); const myModule2 = require("./自定义模块2-2.js"); // console.log(myModule2); console.log(myModule2.user); console.log(myModule2.tag); // myModule2();//还可以new myModule2()表示 new myModule2();
Example: Calculate circle, The area and perimeter of a square
main.js
// 引入模块 let square = require("./square.js"); let circle = require("./circle.js"); // 计算正方形的面积和周长 let squareClass = new square(10); console.log("正方形的面积是:"+squareClass.area()); console.log("正方形的周长是:"+squareClass.circumference()); // 计算圆的面积和周长 console.log("圆的面积是:"+circle.area(5)); console.log("圆的周长是:"+circle.circumference(5));
square.js
//计算正方形的面积和周长,使用模块 module.exports = class { constructor(w){ this.w = w; } area(){ return this.w ** 2; } circumference(){ return this.w * 4; } }
circle.js
//计算圆的面积和周长 exports.area = function (r) { return Math.PI * (r ** 2); } exports.circumference = function (r) { return 2 * Math.PI *r; }
Related recommendations:
Introduction to the content of the module path in Node.js
Code implementation of file monitoring in the Node.js file system
The above is the detailed content of Introduction to the content in Node.js custom modules (with code). For more information, please follow other related articles on the PHP Chinese website!

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment