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
Why do we need master-slave replication? 1. In a ...
Query the current date SELECT CURRENT_DATE(); SEL...
Achieve results Implementation Code html <h1 c...
This article shares the specific code for impleme...
When the server needs to be started during develo...
public function json_product_list($where, $order)...
Table of contents 1. Introduction 2. select 2.1 Q...
A jQuery plugin every day - jQuery plugin to impl...
Previously, we all used files with the suffix .ms...
Preface This article mainly introduces the method...
Since Zabbix version 3.0, it has supported encryp...
Table of contents 1. Arithmetic operators 2. Comp...
MyISAM, a commonly used storage engine in MySQL c...
In this post, we’ll use the :placeholder-shown ps...
Key Points The CSS resize property allows you to ...