-
Notifications
You must be signed in to change notification settings - Fork 1
/
counterII.js
65 lines (49 loc) · 1 KB
/
counterII.js
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
56
57
58
59
60
61
62
63
64
65
//Method 1
/*
var createCounter = function(init) {
let presentCount = init;
function increment() {
return ++presentCount;
}
function decrement() {
return --presentCount;
}
function reset() {
return (presentCount = init);
}
return { increment, decrement, reset };
};
*/
//Method 2
var createCounter = function(init) {
let presentCount = init
return {
increment:()=> ++presentCount,
decrement:()=> --presentCount,
reset:()=> presentCount = init,
}
};
//Method 3
/*
class Counter {
constructor(init) {
this.init = init;
this.presentCount = init;
}
increment() {
this.presentCount += 1;
return this.presentCount;
}
decrement() {
this.presentCount -= 1;
return this.presentCount;
}
reset() {
this.presentCount = this.init;
return this.presentCount;
}
}
var createCounter = function(init) {
return new Counter(init);
};
*/