What is js modularization? JS modularity is not a concept that existed from the beginning, but the requirements for engineering are getting higher and higher, and the need for JS module development has emerged. Therefore, there is about js module development. Let's take a look at some examples of JavaScript modularization.
Modular development history
JS did not have the concept of modularity at the beginning. Until ajax was proposed, the front end could request data from the back end. The front end logic became more and more complex, and many problems appeared. Problem: Global variables, function name conflicts, dependencies are difficult to handle.
At that time, sub-execution functions were used to solve these problems. For example, the classic jquery used anonymous self-executing functions to encapsulate the code and mounted them all under the global variable jquery.
CommonJs
CommonJs is developed through nodeJs. Each js file is a module, and each module has a separate scope. The module uses module.exports as the export and outputs an object. Use the require method to read the file and return its internal module.exports object.
The problem with CommonJs is that its loading is synchronous, which is normal on the server side, but does not apply in a browser full of asynchronous. The community was divided over adapting to the browser.
AMD
AMD is Asynchronous Module Definition. The Chinese name means asynchronous module definition. It is a specification for modular development on the browser side. Since it is not natively supported by JavaScript, the author of AMD personally implemented RequireJS that complies with the AMD specification.
The AMD specification stipulates that the global function define is used to define the module. The usage is define(id, dependencies, factory); where id is the module identifier, dependencies is an array, and inside the array are other modules that the module depends on, and factory is An anonymous function, which contains the logic of the module.
The company currently uses AMD specifications.
For example:
//main.jsrequire(['a', 'b'], function(a, b){ console.log('main.js执行'); a.hello(); $('#b').click(function(){ b.hello(); }); })
The problem with requireJs is that when added to a module, all dependent modules of the module will be preloaded, but these dependencies are likely not to be used at the beginning. It is also very troublesome to write a long list of dependencies at the same time. What's better is that AMD retains the three functions of require, exprots, and module in commonJs. You don't need to write them all in dependencies, but use require to introduce them when needed.
CMD
Since requirejs has all the above-mentioned inelegant aspects, there must be new things to improve it. This is the rising star seajs. The author of seajs is a domestic Taobao front-end trailblazer. Uncle Yu. seajs fully embraces the Modules/Wrappings specification and does not use the callback method to write modules like requirejs. And it does not completely follow the Modules/Wrappings specification. seajs does not use declare to define modules, but uses the same define as requirejs. Maybe the author prefers this name. (However, this will more or less cause confusion in people’s understanding). The way to define a module using seajs is as follows:
//main.jsdefine(function(require, exports, module){ console.log('main.js执行'); var a = require('a'); a.hello(); $('#b').click(function(){ var b = require('b'); b.hello(); }); });
When defining a module, there is no need to list the dependency array, and formal parameters need to be passed in the factory function. require, exports, module, and then it will call the toString method of the factory function, perform regular matching on the content of the function, and analyze the dependencies through the matched require statement, thus truly realizing commonjs style code.
The difference between AMD and CMD
The most obvious difference between AMD and CMD is the different handling of dependencies when defining modules
AMD advocates pre-dependency, which is required when defining modules Declare the modules it depends on
CMD recommends nearby dependencies, and only require when a certain module is used
This difference has its own advantages and disadvantages, it is just a syntactic gap, and both requireJS and SeaJS support each other. Writing method
Future-oriented ES6 module standard
ES6 considers modularity and uses import and export, but currently browsers do not support it, and this standard is only a prototype.
Related recommendations:
Encapsulate a method in js - modularization
First contact with JS require.js Modular tools
The above is the detailed content of What is js modularization? Introduction to js modularization. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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.

Dreamweaver CS6
Visual web development tools

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

Zend Studio 13.0.1
Powerful PHP integrated development environment