-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusecase_clouser.ts
55 lines (45 loc) · 958 Bytes
/
usecase_clouser.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// オブジェクトのプロパティで書くパターン
const Counter = {
cnt: 0,
CountUp: ()=>{
Counter.cnt += 1;
console.log(Counter.cnt)
}
}
// クロージャを活用するパターン
const NewCounter = (function(){
let cnt = 0;
return function () {
cnt += 1;
console.log(cnt);
};
})();
// 活用1
const TakeNumCounter = (function(){
let cnt = 0;
return function (num: number) {
cnt += num;
console.log(cnt);
};
})();
// 活用2
const MakeCounter = function (num:number) {
let current = 0;
return function () {
current = current + num;
console.log(current + "da");
};
};
const Incrementer = MakeCounter(1)
const Decrementer = MakeCounter(-1)
Counter.CountUp();
Counter.CountUp();
Counter.CountUp();
NewCounter()
NewCounter()
NewCounter()
TakeNumCounter(5)
TakeNumCounter(10)
TakeNumCounter(15)
Incrementer()
Decrementer()