File tree Expand file tree Collapse file tree 1 file changed +11
-11
lines changed
docs/1-trial-session/09-functions Expand file tree Collapse file tree 1 file changed +11
-11
lines changed Original file line number Diff line number Diff line change @@ -124,11 +124,16 @@ increment();
124124
125125:::
126126
127- ## モジュール化
127+ ## パーツに分割する
128128
129- 複雑な操作を <Term type =" javascriptFunction " >関数</Term > として <Term strong type =" javascriptModularization " >モジュール化</Term > して複数のブロックに分解することで、コードの可読性を上げることができます。
129+ 複雑な操作を複数の <Term type =" javascriptFunction " >関数</Term > ブロックに分解することで、コードの可読性を上げることができます。この操作を <Term strong type =" javascriptModularization " >モジュール化</Term > と呼びます。
130+ パーツに分割すると、次のようなメリットがあります。
130131
131- モジュール化前:
132+ - ブロックあたりのコードが短くなるので、読みやすい
133+ - パーツごとにテストができるので、デバッグがしやすい
134+ - 汎用性のあるパーツなら、使いまわしができる
135+
136+ 以下の例では、階段を表示する操作の中の、文字列を繰り返す操作を ` repeat ` 関数というパーツに分けています。
132137
133138``` javascript
134139const stringToRepeat = " ☆" ;
@@ -142,26 +147,21 @@ for (let i = 0; i < 10; i += 1) {
142147}
143148```
144149
145- モジュール化後:
146-
147150``` javascript
148- function repeat (stringToRepeat , times ) {
151+ function repeat (stringToRepeat , count ) {
149152 let result = " " ;
150- for (let j = 0 ; j < times ; j += 1 ) {
153+ for (let j = 0 ; j < count ; j += 1 ) {
151154 result += stringToRepeat;
152155 }
153156 return result;
154157}
158+
155159for (let i = 0 ; i < 10 ; i += 1 ) {
156160 document .write (repeat (" ☆" , i));
157161 document .write (" <br>" );
158162}
159163```
160164
161- ::: note
162- この例における` repeat ` <Term type =" javascriptFunction " >関数</Term >は、第一<Term type =" javascriptParameter " >引数</Term >の<Term type =" javascriptString " >文字列</Term >を第二<Term type =" javascriptParameter " >引数</Term >回だけ繰り返し<Term type =" javascriptStringConcatenation " >足した</Term >ものを返します。
163- :::
164-
165165---
166166
167167## 基礎課題
You can’t perform that action at this time.
0 commit comments