Skip to content

Commit d6c6af3

Browse files
authored
Merge pull request #4130 from AChapeton/main
#23 - TypeScript & JavaScript
2 parents ae46e19 + a969b96 commit d6c6af3

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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();

0 commit comments

Comments
 (0)