We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fb791c4 commit 3dc74b0Copy full SHA for 3dc74b0
src/creational/singleton/Sample-3.js
@@ -1,7 +1,7 @@
1
const Cat = (function() {
2
let instance
3
return function() {
4
- if (typeof instance !== 'object') {
+ if (!instance) {
5
instance = this
6
}
7
return instance
src/creational/singleton/Sample-4.js
@@ -0,0 +1,18 @@
+// 用代理实现单例模式
+
+function Cat() {}
+const ProxyCat = (function() {
+ let instance
+ return function() {
8
9
+ instance = new Cat()
10
+ }
11
+ return instance
12
13
+})()
14
15
+/*============== 测试代码 ===============*/
16
+const cat1 = new ProxyCat()
17
+const cat2 = new ProxyCat()
18
+console.log(cat1 === cat2) // true
0 commit comments