Home > Web Front-end > JS Tutorial > body text

First contact with JS require.js modular tool

高洛峰
Release: 2016-12-28 14:22:52
Original
1340 people have browsed it

As the functions of the website gradually become richer, the js in the web page has become more and more complex and bloated. The original method of importing js files one by one through script tags can no longer meet the current Internet development model. We need a team A series of complex requirements such as collaboration, module reuse, unit testing, etc.

第一次接触JS require.js模块化工具

RequireJS is a very small JavaScript module loading framework and one of the best implementers of the AMD specification. The latest version of RequireJS is only 14K compressed, which is very lightweight. It can also work with other frameworks. Using RequireJS will definitely improve the quality of your front-end code.

What benefits does requirejs bring?

Official description of requirejs:

RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code. As a module loader for js files, it can also be used in Node and Rhino environments, balabala... This paragraph describes the basic function of requirejs "modular loading". What is modular loading? We will explain one by one in the following pages

Let’s first look at a common scenario and explain how to use requirejs through examples

Normal writing method

index.html:

<!DOCTYPE html>
<html>
 <head>
  <script type="text/javascript" src="a.js"></script>
 </head>
 <body>
  <span>body</span>
 </body>
</html>
Copy after login

a.js:

function fun1(){
 alert("it works");
}
  
fun1();
Copy after login

Maybe you prefer to write it like this

(function(){
 function fun1(){
  alert("it works");
 }
  
 fun1();
})()
Copy after login

The second method uses block scope to declare function to prevent contamination of global variables. The essence is still the same. I don’t know if you noticed when running the above two examples. When alert is executed, the html content is blank, that is, body is not displayed. It only appears after clicking OK. This is The result of JS blocking browser rendering.

requirejs writing method

Of course, you must first go to the requirejs website to download js -> requirejs.rog

index.html:

<!DOCTYPE html>
<html>
 <head>
  <script type="text/javascript" src="require.js"></script>
  <script type="text/javascript">
   require(["a"]);
  </script>
 </head>
 <body>
  <span>body</span>
 </body>
</html>
Copy after login

a.js:

define(function(){
 function fun1(){
  alert("it works");
 }
  
 fun1();
})
Copy after login

The browser prompts "it works", indicating that the operation is correct, but there is one difference. This time the browser is not blank, the body has appeared in the page, so far we can know that requirejs has the following advantages:

1. Prevent js loading from blocking page rendering

2. Use program calls to load js to prevent the following ugly scenes

<script type="text/javascript" src="a.js"></script>
<script type="text/javascript" src="b.js"></script>
<script type="text/javascript" src="c.js"></script>
<script type="text/javascript" src="d.js"></script>
<script type="text/javascript" src="e.js"></script>
<script type="text/javascript" src="f.js"></script>
<script type="text/javascript" src="g.js"></script>
<script type="text/javascript" src="h.js"></script>
<script type="text/javascript" src="i.js"></script>
<script type="text/javascript" src="j.js"></script>
Copy after login

The above is the entire content of this article, I hope you can understand it The require.js modular tool helps.


For more articles related to the JS require.js modular tool for the first time, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!