


Javascript Modular Programming (3) Introduction to the Usage and Functions of require.js_Basic Knowledge
Parts 1 and 2 of this series introduced Javascript module prototypes and theoretical concepts, and today we introduce how to use them in practice.
I use a very popular library require.js.
1. Why use require.js?
In the earliest days, all Javascript codes were written in one file, and it was enough to load this one file. Later, there were more and more codes, and one file was no longer enough. It had to be divided into multiple files and loaded in sequence. I believe many people have seen the web page code below.
< script src="4.js">
This code loads multiple js files in sequence.
This way of writing has big disadvantages. First of all, when loading, the browser will stop rendering the web page. The more files are loaded, the longer the web page will lose response. Secondly, due to the dependencies between js files, the loading order must be strictly guaranteed (such as the above example) 1.js should be in front of 2.js), and the module with the greatest dependency must be loaded last. When the dependencies are complex, code writing and maintenance will become difficult.
require.js was born to solve these two problems :

(1) Implement asynchronous loading of js files to avoid web page loss of response;
(2) Manage dependencies between modules to facilitate code writing and maintenance.
2. Loading require.js
The first step to use require.js is to download the latest version from the official website.
After downloading, it is assumed that it is placed under the js subdirectory and it can be loaded.
Some people may think that loading this file may also cause the web page to lose response. There are two solutions, one is to load it at the bottom of the web page, the other is to write it as follows:
The async attribute indicates this file It needs to be loaded asynchronously to avoid the web page becoming unresponsive. IE does not support this attribute and only supports defer, so defer is also written.
After loading require.js, the next step is to load our own code. Assume that our own code file is main.js and is also placed under the js directory. Then, just write it as follows:
< script src="js/require.js" data-main="js/main">
The data-main attribute is used to specify the main module of the web program. In the above example, it is main.js under the js directory. This file will be loaded by require.js first. Since the default file extension of require.js is js, main.js can be abbreviated to main.
3. How to write the main module
The main.js in the previous section, I call it the "main module", which means the entry code of the entire web page. It's a bit like the main() function in C language, all code starts running from here.
Let’s see how to write main.js.
If our code does not depend on any other modules, we can write javascript code directly.
// main.js
alert("Loading Success! ");
But in this case, there is no need to use require.js. A really common situation is that the main module depends on other modules, in which case the require() function defined by the AMD specification must be used.
// main.js
require([' moduleA', 'moduleB', 'moduleC'], function (moduleA, moduleB, moduleC){
// some code here
});
require() function accepts two parameters. The first parameter is an array, indicating the modules it depends on. The above example is ['moduleA', 'moduleB', 'moduleC'], that is, the main module depends on these three modules; the second parameter is a callback function. Currently It will be called after all the modules specified above are loaded successfully. Loaded modules will be passed into this function as parameters, so these modules can be used inside the callback function.
require() loads moduleA, moduleB and moduleC asynchronously, and the browser will not lose response; the callback function it specifies will only run after the previous modules are loaded successfully, solving the dependency problem.
Below, we look at a practical example.
Assuming that the main module depends on the three modules of jquery, underscore and backbone, main.js can be written like this:
require(['jquery', 'underscore', 'backbone'], function ($, _, Backbone){
// some code here
} );
require.js will first load jQuery, underscore and backbone, and then run the callback function. The code of the main module is written in the callback function.
4. Module loading
In the last example of the previous section, the dependent modules of the main module are ['jquery', 'underscore', 'backbone']. By default, require.js assumes that these three modules are in the same directory as main.js, with the file names being jquery.js, underscore.js and backbone.js, and then loads them automatically.
Using the require.config() method, we can customize the loading behavior of the module. require.config() is written at the head of the main module (main.js). The parameter is an object, and the paths attribute of this object specifies the loading path of each module.
require.config({
Paths: {
"jquery": "jquery.min",
"underscore": "underscore.min",
"backbone": "backbone.min"
});
Paths: {
"jquery": "lib/jquery.min",
"underscore": "lib/underscore.min",
"backbone": "lib/backbone.min"
}
});
baseUrl: "js /lib",
paths: {
"jquery": "jquery.min",
"underscore": "underscore.min",
"backbone": "backbone.min"
}
});
paths: {
"jquery": "https://ajax .googleapis.com/ajax/libs/jquery/1.7.2/jquery.min"
}
});
require.js requires that each module is a separate js file. In this case, if multiple modules are loaded, multiple HTTP requests will be issued, which will affect the loading speed of the web page. Therefore, require.js provides an optimization tool. After the module is deployed, you can use this tool to merge multiple modules into one file to reduce the number of HTTP requests.
5. How to write AMD modules
The modules loaded by require.js adopt AMD specifications. In other words, the module must be written according to AMD's regulations.
Specifically, the module must be defined using a specific define() function. If a module does not depend on other modules, it can be defined directly in the define() function.
Assume that there is a math.js file, which defines a math module. Then, math.js should be written like this:
// math.js
define(function (){
var add = function (x,y){
return x y;
};
return {
add: add
};
});
The loading method is as follows:
// main.js
require(['math'], function (math){
alert(math.add(1,1));
}) ;
If this module also depends on other modules, then the first parameter of the define() function must be an array indicating the dependencies of the module.
define(['myLib'], function(myLib) {
Function foo(){
myLib.doSomething();
return {
return { foo : foo
};
});
When the require() function loads the above module, the myLib.js file will be loaded first.
Theoretically, modules loaded by require.js must be modules defined with the define() function in accordance with AMD specifications. But in fact, although some popular function libraries (such as jQuery) already comply with the AMD specification, many more libraries do not. So, can require.js load non-standard modules?
The answer is yes. Before loading such modules with require(), you must first use the require.config() method to define some of their characteristics. For example, the two libraries underscore and backbone are not written using AMD specifications. If you want to load them, you must first define their characteristics.
exports: '_'
| },
'backbone': {
| 'jquery'],
bone '
}
}
});
require.config() accepts a configuration object. In addition to the paths attribute mentioned earlier, this object also has a The shim attribute is specially used to configure incompatible modules. Specifically, each module must define (1) the exports value (output variable name), which indicates the name of the module when called externally; (2) the deps array, which indicates the dependencies of the module.
For example, the jQuery plug-in can be defined like this:
exports: 'jQuery.fn.scroll'
}
}
7. require.js plug-in
require.js also provides a series of plug-ins to implement some specific functions. The domready plug-in allows the callback function to run after the page DOM structure is loaded.
The text and image plug-ins allow require.js to load text and image files.
define([
text!review.txt ',
'image!cat.jpg'
],
function(review,cat){
console.log(review);
document.body.appendChild(cat);
}
);
Similar plug-ins include json and mdown, which are used to load json files and markdown files.
(End)

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

Python is a very powerful programming language, and many programmers choose Python as their main programming language. However, too much function nesting in the code can make the program difficult to maintain and understand. This article will explore how to solve the excessive function nesting error in Python code. A brief introduction to function nesting Function nesting refers to the process of defining another function in the body of a function. Function nesting can make the structure of the program clearer and the code easier to read and maintain. However, too many nested functions can lead to an overly complex code structure.

Usage of require: 1. Introduce modules: In many programming languages, require is used to introduce external modules or libraries so that the functions they provide can be used in the program. For example, in Ruby, you can use require to load third-party libraries or modules; 2. Import classes or methods: In some programming languages, require is used to import specific classes or methods so that they can be used in the current file; 3. Perform specific tasks: In some programming languages or frameworks, require is used to perform specific tasks or functions.

With the development of Golang in recent years, it has become one of the programming languages that is gradually recognized by the public. Among them, Golang also has its strong advantages in functional programming and modular programming. In this article, we will conduct an in-depth analysis of the advantages, disadvantages, and application scenarios of Golang functional programming and modular programming. Golang Functional Programming Functional programming is a relatively recent programming paradigm. It mainly emphasizes that functions are first-class citizens of the programming language and can be passed and manipulated like other values. A manifestation of functional programming

Steps to resolve fatalerror:require():Failedopeningrequired'data/tdk.php'(include_path='.;C:phppear') in PHP header When developing websites or applications using PHP, we often encounter various errors . One of the common errors is "fatalerror:require():Failed

In recent years, with the rapid development of Internet technology, PHP, as an efficient, powerful and flexible web programming language, has been adopted by more and more developers. However, as a server-side language, PHP code maintenance and management is a huge challenge when dealing with large projects. In order to solve this problem, PHP developers began to adopt the idea of modular design to build complex applications. This article will introduce modular design in PHP in detail, including the advantages of modular design, methods to implement modular design, and modular design

Steps to resolve FatalError:require():Failedopeningrequired'data/tdk.php' in PHP header When developing and maintaining PHP websites, we often encounter various errors and exceptions. One of the common errors is "FatalError:require():Failedopeningrequired'data/tdk.php'".

Detailed explanation of the role and usage of the require keyword in PHP In PHP development, require is a very commonly used keyword. Its function is to include the specified file for use by the current script. This article will explain in detail the function and use of the require keyword. 1. The role of the require keyword The require keyword can include the contents of a file into the current script. It is usually used to include some necessary external files, such as library files, configuration files, etc. Use req
