当前位置:网站首页>Modular notes
Modular notes
2022-06-11 07:05:00 【Boiled wine cos】
List of articles
Modular notes
modular
overall situation function Pattern
- It can encapsulate different functions into different global functions
- But easy to pollute
namespace Pattern
Simple object encapsulation
let obj = { message :'module2' foo(){ console.log('foo()',this.message) } }But it is still easy to modify
IIFE Pattern
Wrapping with a closure function , And immediately execute the function
(function(window){ let message ='moudle' function foo(){ console.log('foo()',message) } window.moudle = { foo} // Bound to a global object })(window)Call directly moule.foo() Just use it
IIFE Mode enhancement
How to enhance : Introduce dependencies
This is the cornerstone of modern modular implementation
(function(window,$){
let message ='moudle'
function foo(){
console.log('foo()',message)
}
window.moudle = {foo}
// Bound to a global object
$('body').css('background','red')
})(window,jquery)
load script
- One page needs to introduce multiple js file
- problem
- Too many requests
- Rely on ambiguity
- Difficult to maintain
- These problems can be solved by modern modular programming and project construction
- commonjs
commonjs
- standard
- explain
- website .....
- Each file can be treated as a module
- On the server side : Modules are loaded synchronously at runtime ( Will wait for , It's equivalent to blocking
- On the browser side : Modules need to be compiled and packaged in advance
- Basic grammar
- Exposure module
- exports.xxx = value( Any data type )
- moudle.exports = value
- What is the exposed module ?
- All are exports object
- Original time {} Empty objects
- The first time to exports Add attributes , The second one is for the module exports Object Assignment
- Introduce modules
- require(xxx)
- Third-party module :xxx For the module name ( Namely npm The package name )
- Custom module :xxx Is the module file pathname
- Exposure module
- explain
- Realization
- Server side implementation
- node.js
- http://nodejs.cn/
- Browser side implementation
- Browserify( Magic Hat ( Amazing
- http://browserify.org/
- Also known as commonjs Browser side packaging tool
- difference Node and Browserify
- Server side implementation
//package.json
{
// There must be two properties
'name':'commonjs_node',
// Not in Chinese , Don't use capital letters
'version':'1.0.0'
}
// Use the terminal to generate :npm init
// You can write an alternative later
commonjs-Node
stay node Operation in environment
Download third-party modules npm install uniq(–save Is the default property
moudle1.exports = { msg:'moudle1' foo(){ console.log(this.msg) } }moudle2.exports = function(){ console.log(moudle) }exports.foo = function(){ consloe.log('foo() moudles3') } exports.bar = function(){ consloe.log('bar() moudles3') } // Continue to expose exports.arr=[1,2,2,3,3]// Introduce third-party modules , On top of the custom library ( Default ) let uniq = require('uniq') // Introduce other modules into let moudle1 = require('./moudle1') let moudle2 = require('./moudle2') let moudle3 = require('./moudle3') moudle1.foo(); moudle2()//moudle2 It's equivalent to a function moudle3.foo() moudle3.bar() let result = uniq(moudle3.arr) console.log(result) //uniq According to the first digit of the array, the code of waking up
commonjs-Browserify
stay index.html Operation in environment
dist/build( Meaning of construction ) The directory to package the generated files

download browserify
overall situation npm install browserify -g Local npm install browserify --save -dev // Run dependency and development dependencyDevelopment depends on -dev (devdependencies)
Operational dependency --save( Production environment )(dependencies)
moudle1.exports = { msg:'moudle1' foo(){ console.log(this.msg) } }moudle2.exports = function(){ console.log(moudle) }exports.foo = function(){ consloe.log('foo() moudles3') } exports.bar = function(){ consloe.log('bar() moudles3') }let moudle1 = require('./moudle1') let moudle2 = require('./moudle2') let moudle3 = require('./moudle3') moudle1.foo(); moudle2()//moudle2 It's equivalent to a function moudle3.foo() moudle3.bar()// stay index.html Lower introduction , However, the source files should be packaged ( use browserify
-o Output means output
On the left is the source file
On the right is the output file ,bundle It means packing , In fact, it doesn't matter what to do with file names , But it's better to
AMD
namely requirejs The basic use method
Introduction of module package ( Too many files created , Can't write )
( Show declared dependency injection
CMD
Omit
ES6
Worst fire , So the point is
边栏推荐
- Pytest automated test - easy tutorial (01)
- Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
- Method to determine whether it is an array
- 【迅为干货】龙芯2k1000开发板opencv 测试
- 并发工具类
- Flutter 内外边距
- byte和bit的区别
- [deploy private warehouse based on harbor] 3 deploy harbor
- Heartless sword Chinese English bilingual poem 001 Love
- About daily report plan
猜你喜欢

About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally

Leetcode-141. Linked List Cycle

模块化笔记
![[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen](/img/93/d1014b07c924195e062dc83d69b14a.png)
[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen

ESP32学习笔记(49)——ESP-WIFI-MESH接口使用

双周投融报:资本抢滩元宇宙游戏

迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)

Flutter Container组件

Leetcode hot topic 100 topic 11-15 solution

Starting from scratch (V) realize bullet positioning and animation
随机推荐
Web API、DOM
Shuttle container component
1266_FreeRTOS调度器启动代码实现分析
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
数学方法论的含义和研究意义
Leetcode-104. Maximum Depth of Binary Tree
es5和es6的学习小记
Reconstruction and preheating of linked list information management system (2) how to write the basic logic using linear discontinuous structure?
通过 Ingress 进行灰度发布
Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
[MATLAB image fusion] particle swarm optimization adaptive multispectral image fusion [including source code phase 004]
Flutter 约束容器组件
saltstack的常用模块
ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
Group arrays by a specified size
213. house raiding II
【迅为干货】龙芯2k1000开发板opencv 测试
并发工具类
Cv2.rectangle() picture frame