当前位置:网站首页>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
边栏推荐
- 1300. the array closest to the target value after transforming the array and
- Shutter restraint container assembly
- Quality-aware Feature Aggregation Networkfor Robust RGBT Tracking
- mybaits-puls 在xml文件中写sql语句 会报错 Invalid bound statement (not found):
- SQL query. Only the column name is displayed but not the data
- 商汤科技积极复工,将大力投入数字哨兵的产能和部署
- saltstack部署lnmp
- How to make time planning
- Leetcode-647.Palindromic Substrings
- Analysis of key points and difficulties of ES6 promise source code
猜你喜欢

1266_ Implementation analysis of FreeRTOS scheduler startup code

Difference between byte and bit

【Matlab WSN通信】A_Star改进LEACH多跳传输协议【含源码 487期】

Leetcode hot topic 100 topic 21-25 solution

Open source cartoon server mango

A highly controversial issue

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

教育专家王中泽老师一招解决学生问题
![[deploy private warehouse based on harbor] 3 deploy harbor](/img/cd/be68a430e86b4b23ad93b42a338f00.jpg)
[deploy private warehouse based on harbor] 3 deploy harbor

Shuttle container component
随机推荐
开源漫画服务器Mango
Whether the ZABBIX monitoring host is online
SQL query. Only the column name is displayed but not the data
[并发进阶]——线程池总结
Object. Specific implementation and difference between create() and new
Notice on organizing the application for the first edition of Ningbo key software in 2022
Promises/a+ standard Chinese Translation
Required reading 1: the larger the pattern, the better they know how to speak
教育专家王中泽老师一招解决学生问题
Leetcode-141. Linked List Cycle
Luogu p1091 chorus formation (longest ascending subsequence)
Dynamically change the direction of this
Reconstruction and preheating of linked list information management system (2) how to write the basic logic using linear discontinuous structure?
The difference between arrow function and ordinary function
洛谷P1091合唱队形(最长上升子序列)
saltstack部署zabbix状态文件编写
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Transformer Tracking
教育专家王中泽老师多年经验分享:家庭教育不是附庸品
Group arrays by a specified size