回答編集履歴
1
訂正
test
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
普通にclass処理案件だと思いますが、関数でやるというのはこういうことでしょうか?
|
2
|
+
(以下ソースはだめな例でした)
|
2
3
|
```javascript
|
3
4
|
function useCounter(num){
|
4
5
|
this.count = num;
|
@@ -14,3 +15,20 @@
|
|
14
15
|
counter.add()
|
15
16
|
console.log(counter.count); // 2
|
16
17
|
```
|
18
|
+
※上記、よく考えたらnewしてないのでthisがwindowですね
|
19
|
+
結局newするならクラス処理と同等でした
|
20
|
+
```javascript
|
21
|
+
function useCounter(num){
|
22
|
+
this.count = num;
|
23
|
+
this.add = () => {
|
24
|
+
this.count++;
|
25
|
+
};
|
26
|
+
return this;
|
27
|
+
};
|
28
|
+
|
29
|
+
const counter = new useCounter(0);
|
30
|
+
counter.add()
|
31
|
+
console.log(counter.count); // 1
|
32
|
+
counter.add()
|
33
|
+
console.log(counter.count); // 2
|
34
|
+
```
|