当前位置:网站首页>Monomer mode
Monomer mode
2022-06-09 11:02:00 【Qwe7】
3、 ... and 、 Monomer mode
Monomer pattern is one of the most commonly used design patterns . Monomer mode is to create objects using methods , No matter how many times we create an object, it points to the same
1、 Normal way to create objects
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function notSingle() {
return {
a: 1
}
}
var a = notSingle()
var b = notSingle()
console.log(a === b) // Print false
function notSingle2() {
this.a = 123
}
var a1 = new notSingle2()
var a2 = new notSingle2()
console.log(a1 === b1); // Print false
// Whether using functions , Or in the case of constructors , The objects we create are independent of each other
// In the vast majority of cases , This is also the result we expect , That's fine
// When in some cases , We need the returned data created by them to be the object data that has been created , Instead of creating new objects
</script>
</head>
<body>
</body>
</html>2、 Create objects in monomer mode
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var _unique = null;
function createSingle() {
var obj = {
a: 1
}
if (_unique === null) {
_unique = obj
}
return _unique
}
var a = createSingle()
var b = createSingle()
console.log(a === b) // Print true
var a1 = createSingle()
_unique = null // Modified in the middle _unique Will result in inconsistent returned objects
var b1 = createSingle()
console.log(a1 === b1) // false
var createSingle1 = (function() {
var _unique = null;
function single() {
return {
a: 1
}
}
return function() {
if (_unique === null) {
// _unique = {
// a: 1
// }
_unique = single()
} else {
return _unique
}
}
})()
var a2 = createSingle1()
var b2 = createSingle1()
console.log(a2 === b2)
</script>
</head>
<body>
</body>
</html>边栏推荐
- 叁拾柒- JS 在 Canvas 上尝试分形图形 (一) 画了一个普通箱子图
- 全网详细接口测试ApiPost详细教程(实战),吐血整理
- 【水果识别】基于形态学实现水果识别含Matlab源码
- 【 tgcalls】 suivi et débogage des gestionnaires d'appels 2
- PIC simulation (particle in cell codes) (task a and task C)
- 深耕十年,玄武云科技终于稳坐快消SaaS龙头宝座
- 【光学】 基于matlab模拟光的双缝干涉附GUI
- Qt-Char实现动态波形显示
- 费用最低的证券公司 开户安全吗
- Tensorflow new document publishing: add CLP, dtensor The most advanced model is ready!
猜你喜欢

叁拾贰- NodeJS简单代理池(有完没完?) 之 SuperAgent 使用代理不是 Timeout 的 Timeout

Learning fuzzy from SQL injection to bypass the latest safe dog WAF

使用source tree 误删远程以及本地仓库恢复办法

全局组织结构控制之抢滩登陆

MOFs, metal organic framework materials of folate ligands, are loaded with 5-fluorouracil, sidabelamine, taxol, doxorubicin, daunorubicin and other drugs
[email protected] -199 loaded drug ciprofloxacin"/>[email protected] -199 loaded drug ciprofloxacin

Two Sum

叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素等药物

三拾壹- NodeJS簡單代理池(合) 之 MongoDB 鏈接數爆炸了

WebAssembly 2022调查来啦
随机推荐
【tgcalls】跟踪调试calls的manager们 2
DM 平台管理 - netcore
Thirty two - nodejs simple proxy pool (is it finished?) The SuperAgent uses a timeout that is not a timeout
MOFs, metal organic framework materials of folate ligands, are loaded with 5-fluorouracil, sidabelamine, taxol, doxorubicin, daunorubicin and other drugs
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
[pyhton practice] - batch [Dragon Boat Festival] poster Download
[tgcalls] managers who track and debug calls 2
Harbor正确密码登录不上去
merge sort
Jincang of the National People's Congress won the recognition of "key software enterprises encouraged by the state" again
用80%的工时拿100%的薪水,英国正式开启“四天工作制”试验!
复杂嵌套的对象池(2)——管理单个实例对象的对象池
error NU1202: Package Volo.Abp.Cli 5.2.1 is not compatible with netcoreapp3.1
深耕十年,玄武云科技终于稳坐快消SaaS龙头宝座
Thirty eight JS tried fractal graphics on canvas (II) tried mountain building, painted mountains and the basis of angular geometry
【图像增强】基于稀疏表示和正则化实现图像增强附matlab代码
[fruit recognition] fruit recognition based on morphology, including Matlab source code
Comparison between model sorting fields in laraveladmin
投稿开奖丨轻量应用服务器征文活动(4月)奖励公布
One question per day -1200 Minimum absolute difference