当前位置:网站首页>9、 Delegation mode
9、 Delegation mode
2022-06-10 10:09:00 【Qwe7】
Nine 、 Delegate pattern
When multiple objects need to process the same request , These requests can be handled by another object at the same time
1、 Example of delegation mode
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
window.onload = function () {
// var aLis = document.getElementsByTagName('li')
// for( var i = 0; i < aLis.length; i++) {
// aLis[i].onclick = function () {
// console.log(i); // Every time I print 4
// }
// }
// There are two ways to solve variable Promotion
// ES6 let
// let aLis = document.getElementsByTagName('li')
// for(let i = 0; i < aLis.length; i++) {
// aLis[i].onclick = function () {
// console.log(i);
// }
// }
// Closure
// var aLis = document.getElementsByTagName('li')
// for(var i = 0; i < aLis.length; i++) {
// (function (i) {
// aLis[i].onclick = function () {
// console.log(i);
// }
// })(i)
// }
// // There is a problem with this way
// // 1. Every need li Bind different events on , It will cause performance loss and memory occupation
// // 2. Newly added li You also need to rebind events
// var oUl = document.querySelector('ul')
// var oLi = document.createElement('li')
// oLi.innerHTML = 'eeeee'
// oUl.appendChild(oLi)
// oLi.onclick = function () {
// // xxx
// }
var oUl = document.querySelector('ul')
oUl.onclick = function (e) {
var e = e || window.event,
target = e.target || e.srcElement; // IE Compatible processing of
if (target.nodeName.toLowerCase() === 'li') {
console.log(target.innerHTML);
}
}
var oLi = document.createElement('li')
oLi.innerHTML = 'eeeee'
oUl.appendChild(oLi)
}
</script>
</head>
<body>
<ul id="ul1">
<li>aaaa</li>
<li>bbbb</li>
<li>cccc</li>
<li>dddd</li>
</ul>
</body>
</html>Ten 、 Data access object mode
Data access object pattern is mainly used to abstract and encapsulate an object to access and store data sources , This can facilitate the management of data , And avoid duplication between data . Coverage and other problems appear .
1、 Data access object pattern example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
/*
{
nameSpace|key: expire|value
}
*/
var DataVisitor = function (nameSpace, splitSign) {
this.nameSpace = nameSpace;
this.splitSign = splitSign || '|';
}
DataVisitor.prototype = {
status: {
SUCCESS: 0,
FAILURE: 1,
OVERFLOWER: 2, // out of memory
TIMEOUT: 3,
},
getKey: function (key) {
return this.nameSpace + this.splitSign + key
},
set: function (key, value, cbFn, expireTime) {
var status = this.status.SUCCESS
// this.key = this.getKey(key)
// this.expireTime = typeof expireTime === 'number' ? expireTime + new Date().getTime() : -1;
key = this.getKey(key)
expireTime = typeof expireTime === 'number' ? expireTime + new Date().getTime() : -1;
try{
window.localStorage.setItem(key, expireTime + this.splitSign + value)
} catch(e) {
status = this.status.OVERFLOWER
}
cbFn && cbFn.call(this, status, key, value)
return value
},
get: function (key, cbFn) {
key = this.getKey(key);
var status = this.status.SUCCESS
var value = window.localStorage.getItem(key)
if (value) {
var index = value.indexOf(this.splitSign),
time = value.slice(0, index);
if (time > new Date().getTime() || time == -1) {
value = value.slice(index + this.splitSign.length);
} else {
value = null;
status = this.status.TIMEOUT;
window.localStorage.removeItem(key)
}
} else {
status = this.status.FAILURE
}
cbFn && cbFn.call(this, status, key, value)
return value
},
remove: function (key, cbFn) {
key = this.getKey(key);
var status = this.status.SUCCESS
value = window.localStorage.getItem(key)
if (value) {
value = value.slice(value.indexOf(this.splitSign) + this.splitSign.length);
window.localStorage.removeItem(key)
var status = this.status.SUCCESS
}
cbFn && cbFn.call(this, status, key, value)
}
}
var learnInPro = new DataVisitor('learnInPro')
learnInPro.set('aaa', '123', function (status, key, value) {
console.log(status, key, value);
})
learnInPro.get('aaa', function (status, key, value) {
console.log(status, key, value);
})
learnInPro.remove('aaa', function (status, key, value) {
console.log(status, key, value);
})
learnInPro.set('bbb', '123', function (status, key, value) {
console.log(status, key, value);
}, 1000 * 2)
learnInPro.get('bbb', function (status, key, value) {
console.log(status, key, value);
})
setTimeout(function () {
learnInPro.get('bbb', function (status, key, value) {
console.log(status, key, value);
})
}, 1000 * 3)
</script>
</head>
<body>
</body>
</html>边栏推荐
- 【高考季征文】高考那些事儿,作为过来人我有话要说
- 2022年金属非金属矿山提升机操作考试题库及答案
- How to send asynchronous requests gracefully in wechat applets?
- 八、链模式
- "Nonsense" database primary key design
- 2021 ciscn-pwn 初赛
- Someone used this Tiktok e-commerce marketing method to win an offer with a monthly salary of 2W
- 跟我一起来了解GaussDB(for openGauss)【这次高斯不是数学家】
- 2022年煤矿井下电气考试题库及在线模拟考试
- Lambda ellipsis rule
猜你喜欢

How to handle art record? What materials should be prepared for handling the art record?

Requirements and business model analysis - Requirements 17- requirements management

Uncaught TypeError: Cannot read properties of undefined (reading ‘colspan‘)

HMM details + examples

Using requests library to crawl web pages to obtain data

SQL SERVER Always on 监控脚本与一些听到的误解

62. 不同路径-动态规划

【高考季征文】高考那些事儿,作为过来人我有话要说

【图像去噪】基于matlab BdCNN图像去噪【含Matlab源码 1866期】

QChart笔记1:简单线性图LineSeries
随机推荐
Lambda表达式
5G 联通网管设计思路
Flutter:自定义单选按钮
【高考季征文】高考那些事儿,作为过来人我有话要说
【边缘检测】基于matlab八方向sobel图像边缘检测【含Matlab源码 1865期】
【图像特征提取】基于matlab脉冲耦合神经网络(PCNN)图像特征提取【含Matlab源码 1868期】
5g Unicom network management design idea
Flutter 一行Row中显示RadioListTile
618来袭,大量优惠券生成太耗时?ThreadPoolTaskExecutor线程池帮你来搞定
R language plot visualization: plot to visualize the overlapped normalized histogram (distplot with normal distribution), add the density curve KDE in the histogram, and add the whisker graph at the b
HPCA名人堂成员蒋晓维博士,任职大禹智芯首席科学家
隐私计算重要技术突破!亿级数据密态分析可在10分钟内完成
Example 5 of lambda expression
Using requests library to crawl web pages to obtain data
Why is your next computer a computer? Explore different remote operations
图像处理理论和应用
axure弹框设置
Design of smart home control system (onenet) based on stm32_ two thousand and twenty-two
Notes to docker advanced chapter (7) steps to build a redis three master and three slave cluster case in docker
R语言plotly可视化:plotly可视化箱图、使用quartilemethod参数自定义设置箱图分位数的计算方法(Basic Boxplot)