Function
ํจ์๋ ์ ์ฌํ ๋์์ ํ๋ ์ฝ๋๊ฐ ์ฌ๋ฌ ๊ณณ์์ ํ์ํ ๋๊ฐ ๋ง๋ค. ์๋ฅผ ๋ค์ด ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธ์ด๋ ๋ก๊ทธ์์์ ํ ๋ ์๋ด ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ฃผ๋ ๋์ ๋ฑ. ๋๋ถ์ด, ํจ์๋ ํ๋ก๊ทธ๋จ์ ๊ตฌ์ฑํ๋ ์ฃผ์ '๊ตฌ์ฑ ์์'์ด๋ฉฐ, ์ค๋ณต ์์ด ์ ์ฌํ ๋์์ ํ๋ ์ฝ๋๋ฅผ ์ฌ๋ฌ ๋ฒ ํธ์ถํ ์ ์๋ค.
1. ํจ์ ์ ์ธ๋ฌธ
function ํค์๋, ํจ์ ์ด๋ฆ, ๊ดํธ๋ก ๋๋ฌ์ผ ๋งค๊ฐ๋ณ์๋ฅผ ์ฐจ๋ก๋ก ์จ์ฃผ๋ฉด ๋๋ค. ๋ง์ฝ ๋งค๊ฐ๋ณ์๊ฐ ์ฌ๋ฌ ๊ฐ๋ผ๋ฉด ์ฝค๋ง๋ก ๊ตฌ๋ถํด ์ค๋ค.
์ด์ด์ ํจ์๋ฅผ ๊ตฌ์ฑํ๋ ์ฝ๋์ ๋ชจ์์ธ ํจ์์ ๋ณธ๋ฌธ(body)์ ์ค๊ดํธ๋ก ๊ฐ์ผ๋ค.
function name(parameter1, parameter2) { // ํจ์ ๋ณธ๋ฌธ }
2. ์ง์ญ ๋ณ์
ํจ์ ๋ด์์ ์ ์ธํ ๋ณ์์ธ ์ง์ญ ๋ณ์(local variable)๋ ํจ์ ์์์๋ง ์ ๊ทผํ ์ ์๋ค.
function showMessage() { let message = 'hello'; // ์ง์ญ๋ณ์ console.log(message); } showMessage(); // 'hello' console.log(message); // ReferenceError: message is not defined (message๋ ํจ์ ๋ด ์ง์ญ ๋ณ์์ด๊ธฐ ๋๋ฌธ)
3. ์ธ๋ถ ๋ณ์
ํจ์ ๋ด๋ถ์์ ํจ์ ์ธ๋ถ ๋ณ์์ธ ์ธ๋ถ ๋ณ์(outer variable)์ ์ ๊ทผํ ์ ์์
let userName = 'Ella'; function showMessage() { let message = 'Hello, ' + userName; console.log(message); } showMessage(); // Hello, Ella
์ธ๋ถ ๋ณ์ ์ ๊ทผ ๋ฟ๋ง ์๋๋ผ ์์ ๋ ๊ฐ๋ฅ
let userName = 'Ella' function showMessage() { userName = 'Chloe'; // ์ธ๋ถ ๋ณ์ ์์ let message = 'Hello, ' + userName; console.log(message); } console.log(userName); // ํจ์ ํธ์ถ ์ ์ด๋ฏ๋ก Ella ์ถ๋ ฅ showMessage(); // Hello, Chloe: ํจ์ ํธ์ถ console.log(userName); // ํจ์์ ์ํด Chloe ์ถ๋ ฅ
์ธ๋ถ ๋ณ์๋ ์ง์ญ ๋ณ์๊ฐ ์๋ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉ ๊ฐ๋ฅ. ํจ์ ๋ด๋ถ์ ์ธ๋ถ ๋ณ์์ ๋์ผํ ์ด๋ฆ์ ๊ฐ์ง ๋ณ์๊ฐ ์ ์ธ๋์๋ค๋ฉด, ๋ด๋ถ ๋ณ์๋ ์ธ๋ถ ๋ณ์๋ฅผ ๊ฐ๋ฆผ.
let userName = 'Ella'; function showMessage() { let userName = 'Chloe'; // ๊ฐ์ ์ด๋ฆ์ ๊ฐ์ง ์ง์ญ ๋ณ์ ์ ์ธ let message = 'Hello, ' + userName; // Hello, Chloe: ์ธ๋ถ ๋ณ์์ธ Ella๋ ๋ด๋ถ ๋ณ์์ธ Chloe์ ๊ฐ๋ ค์ง console.log(message); } showMessage(); // ํจ์๋ ๋ด๋ถ ๋ณ์์ธ userName๋ง ์ฌ์ฉ console.log(userName); // Ella : ํจ์๋ ์ธ๋ถ๋ณ์์ ์ ๊ทผํ์ง ์์ผ๋ฏ๋ก ๊ฐ์ด ๋ณ๊ฒฝ๋์ง ์๊ณ Ella ์ถ๋ ฅ
cf. userName์ฒ๋ผ ํจ์ ์ธ๋ถ์ ์ ์ธ๋ ๋ณ์๋ ์ ์ญ๋ณ์(global variable)์ด๋ผ๊ณ ํ๋ฉฐ, ์ง์ญ๋ณ์์ ์ํด ๊ฐ๋ ค์ง์ง๋ง ์๋๋ค๋ฉด ๋ชจ๋ ํจ์์์ ์ ๊ทผ ๊ฐ๋ฅ. ํ์ง๋ง ๋ณ์๋ ์ฐ๊ด๋๋ ํจ์ ๋ด์์๋ง ์ ์ธํ๊ณ , ๋๋๋ก ์ฌ์ฉ์ ๊ถํ์ง ์์.
4. ๋งค๊ฐ ๋ณ์
๋งค๊ฐ๋ณ์(parameter, ์ธ์)๋ฅผ ์ฌ์ฉํ๋ฉด ์์์ ๋ฐ์ดํฐ๋ฅผ ํจ์ ์์ ์ ๋ฌํ ์ ์์.
์๋ ์ฝ๋์์ (*), (**) ๋ก ํ์ํ ์ค์์ ํจ์ ํธ์ถ ์, ํจ์์ ์ ๋ฌ๋ ์ธ์๋ ์ง์ญ๋ณ์ from๊ณผ text์ ๊ฐ๊ฐ ๋ณต์ฌ๋จ. ์ดํ ํจ์๋ ์ง์ญ๋ณ์์ ๋ณต์ฌ๋ ๊ฐ์ ์ฌ์ฉ
function showMessage(from, text) { // ์ธ์: from, text console.log(from + ': ' + text); } showMessage ('Ella', 'Hello'); // Ella: Hello! (*) showMessage ('Chloe', 'Hello'); // Chloe: Hello! (**)
์ ๋ฆฌํ์๋ฉด, ๋งค๊ฐ๋ณ์, ์ธ์(parameter) ๋ ํจ์ ์ ์ธ ๋ฐฉ์ ๊ดํธ ์ฌ์ด์ ์๋ ๋ณ์์ ๊ฐ์ผ๋ก, ์ ์ธ ์ ์ฌ์ฉ๋๋ ์ฉ์ด๋ค. ์ธ์(argument) ๋ ํจ์์ ๋งค๊ฐ๋ณ์์ ์ ๋ฌ๋ ๊ฐ์ ๋งํ๋ฉฐ, ํจ์๋ฅผ ํธ์ถํ ๋ ์ฌ์ฉ๋๋ค. ์ฆ, ํจ์ ์ ์ธ ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋์ดํ๊ณ , ํจ์ ํธ์ถ ์ ์ธ์๋ฅผ ์ ๋ฌํด ํธ์ถํ๋ค.
5. ๊ธฐ๋ณธ๊ฐ
๋งค๊ฐ๋ณ์์ ๊ฐ์ ์ ๋ฌํ์ง ์์๋ ๊ทธ ๊ฐ์ด undefined๊ฐ ๋์ง ์๊ฒ ํ๋ ค๋ฉด ํจ์ ์ ์ธ์ = ๋ฅผ ์ฌ์ฉํด ๊ธฐ๋ณธ๊ฐ(default value)์ ์ค์ ํด ์ฃผ๋ฉด ๋๋ค.
function showMessage(from, text = 'please enter your last name') { console.log(from + " " + text); } showMessage("Ella"); // Ella: please enter your last name: text๊ฐ ๊ฐ์ ์ ๋ฌ๋ฐ์ง ๋ชปํด๋ ๊ธฐ๋ณธ๊ฐ์ด ํ ๋น
์๋์ ๊ฐ์ด ๋ณต์กํ ํํ์๋ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๊ฐ๋ฅํ๋ค.
function showMessage(from, text = anotherFunction()) { // anotherFunction()์ text๊ฐ์ด ์์ ๋๋ง ํธ์ถ๋จ // anotherFunction()์ ๋ฐํ ๊ฐ์ด text์ ๊ฐ์ด ๋จ }
6. ๋งค๊ฐ๋ณ์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ์ ์๋ ๋๋ค๋ฅธ ๋ฐฉ๋ฒ
๊ฐ๋์ ํจ์ ์ ์ธ ์๊ฐ ์๋, ํจ์ ์ ์ธ ํ์ ๋งค๊ฐ๋ณ์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ๋๊ฐ ์์. ์ด๋๋ ํจ์ ํธ์ถ ์ ๋งค๊ฐ๋ณ์๋ฅผ undefined์ ๋น๊ตํด ๋งค๊ฐ๋ณ์๊ฐ ์ ์ ๋ฌ๋์๋์ง๋ฅผ ํ์ธ
function showMessage(text) { // ... if (text === undefined) { // ๋งค๊ฐ๋ณ์ ์๋ต ์ text = '๋น ๋ฌธ์์ด'; } console.log(text); } showMessage(); // ๋น ๋ฌธ์์ด
if๋ฌธ ๋์ ๋ ผ๋ฆฌ ์ฐ์ฐ์ || ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, ์๋ ์์๋ ๋งค๊ฐ๋ณ์ ์๋ต ํน์ ๋น ๋ฌธ์์ด์ด ๋์ด์ค๋ฉด ๋ณ์์ '๋น ๋ฌธ์์ด'์ด ํ ๋น๋๋๋ก ํ๋ค.
function showMessage(text) { text = text || '๋น ๋ฌธ์์ด'; }
7. ๋ฐํ๊ฐ
ํจ์ ํธ์ถ ์, ํจ์๋ฅผ ํธ์ถํ ์๋ฆฌ์ ํน์ ๊ฐ์ ๋ฐํํ๊ฒ ํ ์ ์๋ค. ์ด๋ ์ด ํน์ ๊ฐ์ ๋ฐํ ๊ฐ(return value)๋ผ๊ณ ํ๋ค.
์ง์์ return์ ํจ์ ๋ด ์ด๋์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, ์ด return์ ๋ง๋๋ฉด ํจ์ ์คํ์ ์ฆ์ ์ค๋จ๋๊ณ , ํจ์๋ฅผ ํธ์ถํ ๊ณณ์ ๊ฐ์ ๋ฐํํ๋ค. ์๋ ์์์์๋ ๋ฐํ ๊ฐ์ result์ ํ ๋นํ๋ค.
function sum(a, b) { return a + b; } let result = sum(1, 2); // return์์ ๋ฐํํ ๊ฐ์ result์ ํ ๋น console.log(result); // 3
์๋์ ๊ฐ์ด ํจ์ ํ๋์ ์ฌ๋ฌ ๊ฐ์ return๋ฌธ์ด ์ฌ ์๋ ์๋ค.
function checkAge(age) { if (age >= 18) { return true; } else { return confirm('๋ณดํธ์์ ๋์๋ฅผ ๋ฐ์ผ์ จ๋์?'); } } let age = prompt('๋์ด๋ฅผ ์ ๋ ฅํด ์ฃผ์ธ์', 18); if (checkAge(age)) { alert('์ ์ ํ์ฉ'); } else { alert('์ ์ ์ฐจ๋จ'); }
cf. return๋ฌธ์ด ์๋ ํจ์๋ undefined๋ฅผ ๋ฐํํ๋ค.
function doNothing() { /*empty */ } console.log(doNothing() === undefined); // true
cf. return ์ง์์๋ง ์๋ ๊ฒฝ์ฐ๋ undefined๋ฅผ ๋ฐํํ๋ค. return์ return undefined์ ๋์ผํ๊ฒ ๋์ํ๋ค.
function donNothin() { return; } console.log(doNothing() === undefined); // true
caution) return๊ณผ ๊ฐ ์ฌ์ด์๋ ์ ๋ ์ ์ค์ ๋ฃ์ด ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์๋๋ค. ์๋ฐ์คํฌ๋ฆฝํธ๋ return๋ฌธ ๋์ ์๋์ผ๋ก ์ธ๋ฏธ์ฝ๋ก ์ ๋ฃ๊ธฐ ๋๋ฌธ์ ๋ฐํํ๊ณ ์ ํ๋ ํํ์์ ๋ฐํํ์ง ๋ชปํ๋ค.
// ์๋ฅผ ๋ค๋ฉด ์ด๋ฐ ์ฝ๋๋ฅผ return; (1 + 2 + 3 + 4 + 5 + 6) // ํํ์์ ์ฌ๋ฌ ์ค์ ๊ฑธ์ณ ์์ฑํ๊ณ ์ ํ๋ค๋ฉด, ์๋์ ๊ฐ์ด ์ฌ๋ ๊ดํธ๋ฅผ return๊ณผ ๊ฐ์ ์ค์ ์ฐ๊ฑฐ๋, return์ด ์๋ ์ค์์ ํํ์์ ์์ฑํด์ผ ํ๋ค. return ( 1 + 2 + 3 + 4 + 5 + 6 );
8. ํจ์ ์ด๋ฆ ์ง๊ธฐ
ํจ์๋ ์ด๋ค ๋์์ ์ํํ๊ธฐ ์ํด ์ฝ๋๋ฅผ ๋ชจ์ ๋์ ๊ฒ์ด๋ฏ๋ก, ์ด๋ฆ์ ๋๊ฐ ๋์ฌ์ด๋ฉฐ, ๊ฐ๋ฅํ ์ด๋ค ๋์์ ํ๋์ง ๊ฐ๊ฒฐํ๊ณ ๋ช ํํ๊ฒ ์์ฑํด์ผ ํ๋ค.
๋๋ถ์ด, ํจ์๋ ๋์ ํ๋๋ง ๋ด๋นํ๋ฉฐ, ํด๋น ๋์์ ์ ํํ ์ํํด์ผ ํ๋ ๊ฒ์ด ๋ชฉ์ ์ด๋ค.
ํจ์๊ฐ ์ด๋ค ๋์์ ํ๋์ง ์ถ์ฝํด ์ค๋ช ํ๋ ๋์ฌ๋ฅผ ์ ๋์ด๋ก ๋ถ์ฌ ํจ์ ์ด๋ฆ์ ๋ง๋๋ ๊ฒ์ด ๊ด์ต์ด๋ค. ์๋ฅผ ๋ค๋ฉด,
"showโฆ" - ๋ฌด์ธ๊ฐ๋ฅผ ๋ณด์ฌ์ค
"getโฆ" โ ๊ฐ์ ๋ฐํํจ
"calcโฆ" โ ๋ฌด์ธ๊ฐ๋ฅผ ๊ณ์ฐํจ
"createโฆ" โ ๋ฌด์ธ๊ฐ๋ฅผ ์์ฑํจ
"checkโฆ" โ ๋ฌด์ธ๊ฐ๋ฅผ ํ์ธํ๊ณ ๋ถ๋ฆฐ๊ฐ์ ๋ฐํํจ
9. ํจ์ == ์ฃผ์
ํจ์๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ค๋ฉด ํ ์คํธ์ ๋๋ฒ๊น ์ด ์ฌ์์ง๋ฉฐ, ํจ์ ๊ทธ ์์ฒด๋ก ์ฃผ์์ ์ญํ ๊น์ง ํด๋ธ๋ค. ์ด๋ฅผ '์๊ธฐ ์ค๋ช ์ (self-describing) ์ฝ๋' ๋ผ๊ณ ํ๋ค.
Last updated