Preface: 1. Concept
2. The benefits of modularity
3. Problems after introducing multiple script tags
//index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="jQuery.js"></script> <script src="module.js"></script> </head> <body> <div>123</div> </body> <script> myModule.foo(); myModule.bar(); console.log(myModule.data); myModule.data = 'xxxx'; myModule.foo(); </script> </html> //module.js IIFE (anonymous function self-calling) ;(function(window,$){ let data = "www.baidu.com"; function foo() { console.log(`foo() ${data}`); //You need to use the jQuery library here $('body').css('background', 'red') } function bar() { console.log(`bar() ${data}`); otherFun(); } function otherFun() { console.log(`otherFun()`); } window.myModule = { foo, bar }; })(window, jQuery) 1. CommonJS
Features:
grammar:
The The The loading mechanism of AMD
Using require.js <!-- index.html --> <script src="https://cdn.bootcdn.net/ajax/libs/require.js/2.3.6/require.js"></script> //Define a module1 without dependencies define('module1', () => { let count = 0; const add = () => ++ count; const reset = () => count = 0; const upperCase = string => string.toUpperCase() return { add, reset, upperCase } }) //Define a module2 with dependencies, which depends on module1 define('module2',['module1'], (module1) => { const showMsg = () => module1.upperCase('hello-amd'); return { showMsg } }) <!-- Use the module in the HTML file --> <body> <script> require.config({ paths: { module1: './modules/module1', module2: './modules/module2' } }) require(['module1', 'module2'], (module1, module2) => { console.log(module1.add()) // 1 console.log(module1.reset()) //0 console.log(module2.showMsg()) //HELLO-AMD }) </script> </body> 3. CMD
Using sea.js <script src="https://cdn.bootcdn.net/ajax/libs/seajs/3.0.3/sea.js"></script> //Define module module1 define((require, exports, module) => { let string = 'I am a string'; const readString = () => 'module1 show() ' + string; //Exposing to the outside world exports.readString = readString; }) //Define module module2 define((require, exports, module) => { exports.msg = "It is me" }) //Define module define((require, exports, module) => { //Introduce dependent modules (synchronous) var module1 = require('./module1'); console.log(module1.readString()) // module1 show() I am a string //Introduce dependent modules (asynchronous) require.async('./module2', md2 => { console.log(`This is imported asynchronously: ${md2.msg}`) //This is imported asynchronously: It's me}) }) <!-- HTML file uses module --> <body> <script> seajs.use('./modules/module') </script> ES6 modularity The design idea of ES6 modules is to be as static as possible, so that the module dependencies, as well as the input and output variables, can be determined at compile time. Both CommonJS and AMD modules can only determine these things at runtime.
//mian.js export default { showMsg() { console.log('hahahahahah') } } export const msg = "It's a beautiful time with full moon and flowers!" //index.js import module1 from "./module1"; //corresponding to export default module1.showMsg() import { msg } from './module1'; //corresponding to export console.log(msg) /*tips: Don't use <script type="module"> in HTML import ....., there are cross-domain issues, you can download a plug-in in vscode, or start a local service to solve it, I will not go into details. </script>*/ This is the end of this article about the detailed explanation of JavaScript modularization. For more relevant JavaScript modularization content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of Mysql's concurrent parameter adjustment
>>: CSS cleverly uses gradients to achieve advanced background light animation
Passing values between mini program pages Good ...
Usage scenarios For existing servers A and B, if ...
<br />Previous article: Web Design Tutorial ...
Preface This article mainly introduces the use an...
Table of contents How to set cookies Disadvantage...
Table of contents 1. Use closures 2. Use ES6 clas...
We know that when using HTML on NetEase Blog, we ...
Preface Since many friends say they don’t have Ma...
Table of contents View all storage engines InnoDB...
1. Environmental Preparation 1.1 Basic Environmen...
When the database concurrently adds, deletes, and...
The installation tutorial of mysql 8.0.20 winx64....
Copy code The code is as follows: <iframe id=&...
Introduction: Sometimes, in order to develop a pr...
yum install httpd php mariadb-server –y Record so...