highlight()ํจ์ ๋ด์ reverse()๋ผ๋ ๋ฉ์๋๋ง ํธ์ถ : ๊ธธ์ด๋ ์ค์ํ์ง ์์.
1. ํจ์๋ฅผ ์๋ก ๋ง๋ค๊ณ ๋ชฉ์ ์ ์ ๋๋ฌ๋ด๋ ์ด๋ฆ์ ๋ถ์ธ๋ค. ('์ด๋ป๊ฒ'๊ฐ ์๋ '๋ฌด์์ ํ๋์ง'๊ฐ ๋๋ฌ๋์ผ ํ๋ค)
-> ๋ชฉ์ ์ด ๋๋ฌ๋๋ ์ด๋ฆ, ์ค์ฒฉ ํจ์๋ฅผ ์ง์ํ๋ ์ธ์ด๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์ถ์ถํ ํจ์๋ฅผ ์๋ ํจ์ ์์ ์ค์ฒฉ, ์ธ์ ๋ ํจ์ ์ฎ๊ธฐ๊ธฐ๋ฅผ ์ ์ฉ.
2. ์ถ์ถํ ์ฝ๋๋ฅผ ์๋ณธ ํจ์์์ ๋ณต์ฌํ์ฌ ์ ํจ์์ ๋ถ์ฌ๋ฃ๋๋ค.
3. ์ถ์ถํ ์ฝ๋ ์ค ์๋ณธ ํจ์์ ์ง์ญ ๋ณ์๋ฅผ ์ฐธ์กฐํ๊ฑฐ๋ ์ถ์ถํ ํจ์์ ์ ํจ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๋ณ์๋ ์๋์ง ๊ฒ์ฌํ๋ค. ์๋ค๋ฉด ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ๋ค.
4. ๋ณ์๋ฅผ ๋ค ์ฒ๋ฆฌํ๋ค๋ฉด ์ปดํ์ผํ๋ค.
5. ์๋ณธ ํจ์์์ ์ถ์ถํ ์ฝ๋ ๋ถ๋ถ์ ์๋ก ๋ง๋ ํจ์๋ฅผ ํธ์ถํ๋ ๋ฌธ์ฅ์ผ๋ก ๋ฐ๊พผ๋ค. (์ฆ, ์ถ์ถํ๋ ํจ์๋ก ์ผ์ ์์ํ๋ค.)
6. ํ ์คํธํ๋ค.
7. ๋ค๋ฅธ ์ฝ๋์ ๋ฐฉ๊ธ ์ถ์ถํ๊ฒ๊ณผ ๋๊ฐ๊ฑฐ๋ ๋น์ทํ ์ฝ๋๊ฐ ์๋์ง ์ดํ๋ค. ์๋ค๋ฉด ๋ฐฉ๊ธ์ถ์ถํ ์ ํจ์๋ฅผ ํธ์ถํ๋๋ก ๋ฐ๊ฟ์ง ๊ฒํ ํ๋ค.
์ง์ญ ๋ณ์๋ค์ ๋งค๊ฐ๋ณ์๋ก ๋๊ธฐ๋ฉด ๋๋ค.
// ๋ณ๊ฒฝ ์ด์ ์ฝ๋
function printOwing(invoice){
let outstanding=0;
printBanner();
for (const o of invoice.orders){
outstanding+=o.amount;
}
const today = Clock.today;
invoice.dueDate = new Date(today.getFullYear(),today.getMonth(),today.getDate()+30);
console.log(`๊ณ ๊ฐ๋ช
:${invoice.customer}`);
console.log(`์ฑ๋ฌด์ก :${outstanding}`);
console.log(`๋ง๊ฐ์ผ : ${invoice.dueDate.toLocaleDateString()}`);
}
// ๋ณ๊ฒฝ ํ
function printOwing(invoice){
let outstanding = 0;
printBanner();
for (const o of invoice.orders){
outstanding+=o.amount;
}
// ์ง์ญ๋ณ์๊ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ผ๋ฉด ๋๊ฐ์ด ๋งค๊ฐ๋ณ์๋ก ๋๊ธด ํ ํ๋ ๊ฐ์ ์์
recordDueDate(invoice);
// ์ง์ญ๋ณ์๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ
printDetails(invoice,outstanding);
}
function recordDueDate(invoice){
const today = Clock.today;
invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate()+30);
}
function printDetails(invoice,outstanding){
console.log(`๊ณ ๊ฐ๋ช
:${invoice.customer}`);
console.log(`์ฑ๋ฌด์ก :${outstanding}`);
console.log(`๋ง๊ฐ์ผ : ${invoice.dueDate.toLocaleDateString()}`);
}
๋งค๊ฐ๋ณ์์ ๊ฐ์ ๋์ ํ๋ ์ฝ๋๋ฅผ ๋ฐ๊ฒฌํ๋ฉด,
์์ ๋ณ์๋ฅผ ์๋ก ํ๋ ๋ง๋ค์ด ๋ณ์์ ๋์ ํ๋๋ก ํ๋ค.
// 1) let outstanding = 0;์ ๊ด๋ จ ์๋ ์ฝ๋ ๊ทผ์ฒ๋ก ์ฎ๊ธด๋ค.
function printOwing(invoice){
printBanner();
let outstanding = 0;
for (const o of invoice.orders){
outstanding+=o.amount;
}
recordDueDate(invoice);
printDetails(invoice,outstanding);
}
// 2) ์ถ์ถํ ์ฝ๋ ๋ณต์ฌ
function printOwing(invoice){
printBanner();
let outstanding = 0;
for (const o of invoice.orders){
outstanding+=o.amount;
}
recordDueDate(invoice);
printDetails(invoice,outstanding);
}
function calculateOutstanding(invoice){
let outstanding = 0;
for (const o of invoice.orders){
outstanding += o.amount;
}
return outstanding;
}
// 3) ์๋ก์ด ํจ์ ํธ์ถ ๋ฐ ๋ฐํ๊ฐ ์ ์ฅ
function printOwing(invoice){
printBanner();
const outstanding = calculateOutstanding(invoice);
recordDueDate(invoice);
printDetails(invoice,outstanding);
}
function calculateOutstanding(invoice){
let outstanding = 0;
for (const o of invoice.orders){
outstanding += o.amount;
}
return outstanding;
}
// 4) ์ฝ๋ฉ์คํ์ผ ๋ณ๊ฒฝ
function printOwing(invoice){
printBanner();
const outstanding = calculateOutstanding(invoice);
recordDueDate(invoice);
printDetails(invoice,outstanding);
}
function calculateOutstanding(invoice){
let result = 0;
for (const o of invoice.orders){
result += o.amount;
}
return result;
}
๋ ์ฝ๋๋ก ๋ฌถ์ด์๋ฐํ : ์์ ๋ณ์๋ฅผ ์ง์ํจ์๋ก ๋ฐ๊พธ๊ฑฐ๋, ๋ณ์๋ฅผ ์ชผ๊ฐ๋ ์์ผ๋ก ์ฒ๋ฆฌ
์๋ณธ ํจ์์ ๊ฐ์ ์์ค์ ๋ฌธ๋งฅ์ผ๋ก ๋จผ์ ์ถ์ถํด๋ณด์
| 6.6. ๋ณ์ ์บก์ํํ๊ธฐ (Encapsulate Variable) (0) | 2021.09.26 |
|---|---|
| 6.3. ๋ณ์ ์ถ์ถํ๊ธฐ (0) | 2021.09.26 |
| 9.2. ํ๋ ์ด๋ฆ ๋ฐ๊พธ๊ธฐ (Rename Field) (0) | 2021.09.25 |
| 6.7. ๋ณ์ ์ด๋ฆ ๋ฐ๊พธ๊ธฐ (Rename Variable) (0) | 2021.09.25 |
| 6.2. ํจ์ ์ธ๋ผ์ธํ๊ธฐ (inline Function) (0) | 2021.09.25 |