Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6dfa016

Browse filesBrowse files
committed
Add examples
1 parent 97bc90b commit 6dfa016
Copy full SHA for 6dfa016

File tree

Expand file treeCollapse file tree

3 files changed

+33
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-0
lines changed

‎JavaScript/1-simple.js

Copy file name to clipboard
+9Lines 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

Copy file name to clipboard
+8Lines 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

Copy file name to clipboard
+16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.