回答編集履歴

7

⑤を復帰値に変更

2020/01/15 13:18

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -84,4 +84,4 @@
84
84
 
85
85
 
86
86
 
87
- ![イメージ説明](65ffe4da7b4596a308932a8ed06e940e.png)
87
+ ![クロージャー処理の流れ](1251ef69b6ee3f074b8b2c69bb1ffd9e.png)

6

水色番号追加

2020/01/15 13:18

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -84,4 +84,4 @@
84
84
 
85
85
 
86
86
 
87
- ![イメージ説明](81924f2a12b71f6660d674c8eb52e4d0.png)
87
+ ![イメージ説明](65ffe4da7b4596a308932a8ed06e940e.png)

5

図を追記

2020/01/15 11:38

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -81,3 +81,7 @@
81
81
  increment(); //4 : 別の変数であることの確認
82
82
 
83
83
  ```
84
+
85
+
86
+
87
+ ![イメージ説明](81924f2a12b71f6660d674c8eb52e4d0.png)

4

コードにコメント追加

2020/01/15 11:32

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
 
62
62
 
63
- let increment = make_increment();
63
+ let increment = make_increment(); // make_increment関数を呼び出して do_increment 関数を作り increment変数に代入
64
64
 
65
65
  increment(); //1
66
66
 
@@ -70,9 +70,9 @@
70
70
 
71
71
 
72
72
 
73
- let increment2 = make_increment();
73
+ let increment2 = make_increment(); // increment とは別の count変数と do_increment関数を作る
74
74
 
75
- increment2(); //1 : increment とは別の領域に count変数と do_increment関数が作られる
75
+ increment2(); //1
76
76
 
77
77
  increment2(); //2
78
78
 

3

コードにコメント追加

2020/01/15 11:06

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -36,15 +36,15 @@
36
36
 
37
37
  ```javascript
38
38
 
39
- function make_increment () {
39
+ function make_increment () { // 関数定義により関数オブジェクトが作られる
40
40
 
41
- let count;
41
+ let count; // 関数を呼び出すと変数領域が作られる
42
42
 
43
43
 
44
44
 
45
- function do_increment () {
45
+ function do_increment () { // 関数定義により関数オブジェクトが作られる
46
46
 
47
- count += 1;
47
+ count += 1; // do_increment() を呼び出したときに +1 される
48
48
 
49
49
  console.log(count);
50
50
 
@@ -52,9 +52,9 @@
52
52
 
53
53
 
54
54
 
55
- count = 0;
55
+ count = 0; // make_increment() を呼び出したときに代入する
56
56
 
57
- return do_increment
57
+ return do_increment // 変数countを抱えた do_increment関数オブジェクトを返す
58
58
 
59
59
  }
60
60
 

2

名前付き関数に書き換えた例を追記

2020/01/15 10:53

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -19,3 +19,65 @@
19
19
  更に、`increment()` でクロージャー(内側のfunction) を呼び出すと `count+=1;` を実行して、外部変数countが `2` に変わり、`return count;` で 2 が帰ります。
20
20
 
21
21
  更に、`increment()` でクロージャー(内側のfunction) を呼び出すと `count+=1;` を実行して、外部変数countが `3` に変わり、`return count;` で 3 が帰ります。
22
+
23
+
24
+
25
+ ---
26
+
27
+
28
+
29
+ 即時関数を名前付き関数にしてみました。
30
+
31
+ こうすることで、make_increment() を何度も呼び出せます。
32
+
33
+ make_increment() を呼び出すごとに 変数 count 領域と do_increment 関数オブジェクト が作られます。
34
+
35
+
36
+
37
+ ```javascript
38
+
39
+ function make_increment () {
40
+
41
+ let count;
42
+
43
+
44
+
45
+ function do_increment () {
46
+
47
+ count += 1;
48
+
49
+ console.log(count);
50
+
51
+ }
52
+
53
+
54
+
55
+ count = 0;
56
+
57
+ return do_increment
58
+
59
+ }
60
+
61
+
62
+
63
+ let increment = make_increment();
64
+
65
+ increment(); //1
66
+
67
+ increment(); //2
68
+
69
+ increment(); //3
70
+
71
+
72
+
73
+ let increment2 = make_increment();
74
+
75
+ increment2(); //1 : increment とは別の領域に count変数と do_increment関数が作られる
76
+
77
+ increment2(); //2
78
+
79
+
80
+
81
+ increment(); //4 : 別の変数であることの確認
82
+
83
+ ```

1

説明追記

2020/01/15 10:48

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -8,9 +8,9 @@
8
8
 
9
9
  この時点では `let count=0;` は実行されません。
10
10
 
11
- increment に代入するときの最後が `();` になっていて即時関数を呼び出しています。
11
+ 変数`increment` に代入するときの処理の最後の部分が `();` になっていて即時関数を呼び出しています。
12
12
 
13
- `let count=0;` を実行して変数`count`が`0`になり、その変数`count`を参照する内側の即時関数functionを作って return し、その即時関数が外部変数`count`を抱えた・閉じ込めた・クローズしたクロージャーとなって変数`increment`に代入されます。
13
+ この呼び出しによって `let count=0;` を実行して変数`count`が`0`になり、その変数`count`を参照する内側の即時関数functionを作って return し、その即時関数が外部変数`count`を抱えた・閉じ込めた・クローズしたクロージャーとなって変数`increment`に代入されます。
14
14
 
15
15
 
16
16