File tree Expand file tree Collapse file tree 2 files changed +94
-0
lines changed Expand file tree Collapse file tree 2 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1+ const counter = ( ) => {
2+ var instance = null ;
3+ var count = 0 ;
4+ function printCount ( ) {
5+ console . log ( "Numero de instancias activas: " + count ) ;
6+ }
7+ function init ( ) {
8+ count ++ ;
9+ return { } ;
10+ }
11+ function createInstance ( ) {
12+ if ( instance == null ) {
13+ instance = init ( ) ;
14+ }
15+ return instance ;
16+ }
17+ function closeInstance ( ) {
18+ count -- ;
19+ instance = null ;
20+ }
21+ return {
22+ createInstance : createInstance ,
23+ closeInstance : closeInstance ,
24+ printCount : printCount
25+ } ;
26+ }
27+ var foo = counter ( ) ;
28+ foo . printCount ( ) ;
29+ foo . createInstance ( ) ;
30+ foo . printCount ( ) ;
31+ foo . createInstance ( ) ;
32+ foo . printCount ( ) ;
33+ foo . createInstance ( ) ;
34+ foo . printCount ( ) ;
35+ foo . closeInstance ( ) ;
36+ foo . printCount ( ) ;
37+ foo . createInstance ( ) ;
38+ foo . printCount ( ) ;
39+ foo . closeInstance ( ) ;
40+ foo . printCount ( ) ;
Original file line number Diff line number Diff line change 1+ interface fooType {
2+ createInstance : ( ) => { } ;
3+ closeInstance : ( ) => void ;
4+ printCount : ( ) => void ;
5+ }
6+
7+ const counter = ( ) => {
8+
9+ let instance : { } | null = null ;
10+ let count : number = 0 ;
11+
12+ function printCount ( ) {
13+ console . log ( "Numero de instancias activas: " + count ) ;
14+ }
15+
16+ function init ( ) {
17+ count ++ ;
18+ return { }
19+ }
20+
21+ function createInstance ( ) {
22+ if ( instance == null ) {
23+ instance = init ( ) ;
24+ }
25+ return instance ;
26+ }
27+
28+ function closeInstance ( ) {
29+ count -- ;
30+ instance = null ;
31+ }
32+
33+ return {
34+ createInstance,
35+ closeInstance,
36+ printCount
37+ }
38+ }
39+
40+ let foo : fooType = counter ( ) ;
41+
42+ foo . printCount ( )
43+ foo . createInstance ( )
44+ foo . printCount ( )
45+ foo . createInstance ( )
46+ foo . printCount ( )
47+ foo . createInstance ( )
48+ foo . printCount ( )
49+ foo . closeInstance ( )
50+ foo . printCount ( )
51+ foo . createInstance ( ) ;
52+ foo . printCount ( ) ;
53+ foo . closeInstance ( ) ;
54+ foo . printCount ( ) ;
You can’t perform that action at this time.
0 commit comments