Since the birth of Javascript, no one has ever regarded it as a programming language. In the Web 1.0 era, this scripting language was mainly used for form validation and web page special effects. It wasn't until the Web 2.0 era, when front-end engineers used it to greatly improve the user experience on web pages, that JS was widely valued. As JS becomes more and more popular, it has roughly gone through changes in tool libraries, component libraries, front-end frameworks, and front-end applications. Javascript inherently lacks one feature: modules, and the emergence of the CommonJS specification makes up for this shortcoming. This article will introduce the CommonJS specification and Node’s module mechanism.
Among other high-level languages, Java has class files, Python has an import mechanism, and PHP has include and require. The way JS introduces code through the <script> tag appears messy. In the past, people had to use namespaces and other methods to artificially constrain the code. It was not until the emergence of the CommonJS specification that the front-end and back-end Javascript could be unified. Node draws on the Modules specification of CommonJS to implement a very easy-to-use module system. </script>
1. CommonJS module specification
The module specification of CommonJS is divided into 3 parts:
1). Module reference: introduce a module's API into the current context through the require() method and pass in a module identifier, such as var math = require('math');
2).Module definition: Export the methods or variables of the current module through the exports object. There is also a module object in the module, and exports are actually attributes of the module. In Node, a file is a module, and the "global variables" within the module are not visible to the outside world. Only the attributes mounted on exports are public, such as exports.add = function() {}; exports.PI = 3.1415926;
3). Module identification: It is actually the parameter passed to require(), such as the above 'math', which must be a string that conforms to the camel nomenclature, or a relative path starting with "." ".." or Absolute path, it can have no filename suffix ".js"
2. Node module implementation process
In Node, modules are divided into two categories: one is the core module provided by Node itself, and the other is the file module written by the user. Part of the core module is compiled into a binary file during the compilation process of Node source code. The core module is loaded directly into the memory when Node starts, so its loading speed is the fastest. The file module is dynamically loaded at runtime and requires three steps: path analysis, file location, and compilation and execution. Note that Node caches imported modules to reduce the cost of secondary introduction, and adopts the strategy of loading from the cache with the highest priority for secondary loading of the same module.
2.1 Path Analysis
Path analysis mainly analyzes the module identifiers mentioned above, which are mainly divided into the following categories:
1), core modules, such as http, fs, path, etc.
2) Relative path file module starting with, . or ..
3). Absolute path file module starting with /
4). Custom file module, which may be in the form of a file or package. Node will try to find the target file one by one according to the module path array module.paths. It usually searches for the directory named node_modules along the current directory step by step until the root directory, so this is the most time-consuming way to find it.
2.2 File Positioning
Based on path analysis, file location needs to pay attention to the following details:
1) File extension analysis: Since the CommonJS specification allows the module identifier to not fill in the extension, Node will fill in the extension in the order of .js, .json, and .node, and try in sequence
2) Directory analysis and packages: If the corresponding file is not found after the above file extension analysis, but a directory is obtained, Node will treat the directory as a package
2.3 Compilation and execution
After locating the specific file, Node will create a new module object, load and compile it according to the path. For different extensions, the loading method is different:
1), .js file: read the file synchronously through the fs module and compile and execute
2). .node file: This is an extension file written in C/C and loaded through the dlopen() method
3).json file: Read the file synchronously through the fs module, and use JSON.parse() to parse and return the result
4). Other extension files: are loaded as .js files
We know that each module file has three variables: require, exports, and module by default. Even in Node’s API documentation, we know that each module also has two variables: filename and dirname. They Where did it come from? How does Node's module ensure that the declared "global variables" do not actually contaminate other modules? In fact, Node will wrap the file contents head and tail during the compilation of JS modules. The following is an example of a JS file with head and tail packaging:
(function(exports, require, module, __filename, __dirname) {
/* The middle is the actual content of the JS file */
var math = require('math');
exports.area = function(radius) {
return Math.PI * radius * radius;
};
/* The actual content of the JS file ends */
});
In this way, each module file is scope-isolated, and variables such as require, exports, and module are also injected into the context of the module. This is Node's implementation of the CommonJS module specification. The compilation process of C/C modules and Node core modules is relatively complicated and will not be described in detail.
3. Module call stack
It is necessary to clarify the calling relationship of various modules in Node, as shown in the figure below:
C/C built-in module is the lowest module and is a core module. It mainly provides APIs for Javascript core modules and third-party Javascript file modules to call. In practice, such modules are almost never exposed. The Javascript core module has two main responsibilities: one is to serve as the encapsulation layer and bridging layer of the C/C built-in module for file module calls, and the other is a purely functional module that does not need to deal with the bottom layer. File modules are usually written by third parties, including ordinary Javascript modules and C/C extension modules.
4. Packages and NPM
4.1 Package Structure
A package is essentially an archive file (usually .zip or .tar.gz), which can be decompressed and restored to a directory after installation. CommonJS's package specification consists of two parts: package structure and package description file. A package structure that fully conforms to the CommonJS specification should contain the following files:
1).package.json: package description file
2).bin: directory where executable binary files are stored
3).lib: Directory to store Javascript code
4).doc: Directory for storing documents
5).test: Directory to store unit test cases
4.2 Package Description File
The package description file is a JSON file - package.json, located in the root directory of the package. It is an important part of the package and is used to describe the general information of the package. All behaviors of NPM to be mentioned later are closely related to the fields of this file. The following uses the package.json file of the well-known Web framework express project as an example to illustrate the meaning of some common fields.
1).name: package name
2).description: package introduction
3).version: version number, which must comply with "semantic version control", refer to http://semver.org/
4).dependencies: List of packages that are required to use the current package. This attribute is very important. NPM will automatically load dependent packages through this attribute
5).repositories: List of locations where source code is hosted
For the usage of other fields, please refer to NPM package.json description
4.3 NPM common functions
NPM (node package manager), usually called node package manager. Its main function is to manage node packages, including: installation, uninstallation, update, view, search, release, etc.
4.3.1 NPM package installation
There are two types of installation of Node packages: local installation and global installation. The difference between the two is as follows:
1). Local installation npm install
2). Global installation npm install -g
4.3.2 NPM package management
The following takes grunt-cli (grunt command line tool) as an example to list commonly used package management commands:
1).npm install: Install all packages declared in the dependencies and devDependencies fields of the package.json file
2).npm install grunt-cli@0.1.9: Install a specific version of grunt-cli
3).npm install grunt-contrib-copy --save: Install grunt-contrib-copy and save the dependency to the package.json file
4).npm uninstall grunt-cli: Uninstall package
5).npm list: Check which packages are installed
6).npm publish

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.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools