-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsingleton2.js
More file actions
35 lines (32 loc) · 818 Bytes
/
Copy pathsingleton2.js
File metadata and controls
35 lines (32 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* --Singleton with single object literal --
good:
* simple and compact
*
bad:
* not delayed
* uses this
* forcing us to use apply, bind or => techniques to nest function depending
* on this
* no private members
* if we want a computed string as a key we have to do it before and
* store it in a variable
* if we want a computed value for a function inside that Object
* we are pretty much forced to create variables at the same level to do the computation
conclusion:
* good for small singletons, or
* good for singletons filled with static stuff (e.g string literals)
* bad for the rest
*/
"use strict";
const game = {
score: 0,
setScore: function (newScore) {
this.score = newScore;
},
isGameOver: function () {
return this.score > 100;
}
};
/*usage:
game.setScore(30);
*/