Skip to content

Commit 6dfa016

Browse files
committed
Add examples
1 parent 97bc90b commit 6dfa016

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

JavaScript/1-simple.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
function Singleton() {
4+
const instance = Singleton.instance;
5+
if (instance) return instance;
6+
Singleton.instance = this;
7+
}
8+
9+
console.assert(new Singleton() === new Singleton());

JavaScript/2-safe.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const Singleton = new (function() {
4+
const single = this;
5+
return function() { return single; };
6+
})();
7+
8+
console.assert(new Singleton() === new Singleton());

JavaScript/3-complex.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const Singleton = (function() {
4+
let instance;
5+
6+
function Singleton() {
7+
if (instance) return instance;
8+
instance = this;
9+
}
10+
11+
Singleton.prototype.test = function() {};
12+
13+
return Singleton;
14+
})();
15+
16+
console.assert(new Singleton() === new Singleton());

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy