function? | -ํ๋ก๊ทธ๋จ์ ํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ building block -subprogram ์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ฉฐ ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค -ํ๊ฐ์ง์ ์์ ์ด๋ ์ด๋ค๊ฐ(value)์ ๊ณ์ฐํ๊ธฐ ์ํด ์ฐ์ |
์ฌ์ฉ๋ฒ | function ํจ์์ด๋ฆ(ํ๋ผ๋ฏธํฐ1, ํ๋ผ๋ฏธํฐ2) { ๋ด์ฉ } ๋ฆฌํด; ํ๋์ function์ === ํ๊ฐ์ง์ผ๋ง ํ๋ค. |
ํจ์์ด๋ฆ ์์ฑ๋ฒ | ๋ณ์๋ ๋ช
์ฌ๋ก ํจ์๋ ๋์ฌ๋ก (doSomething) |
ํจ์๋ object ์ด๋ค. | 1. ๋ณ์์ ํ ๋น์ด 2. function ์ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌ 3. ๋ฆฌํด๊ฐ์ผ๋ก๋ ๋ฆฌํด |
1. function
function ์ผ๋ก printHelloํจ์๋ฅผ ๋ง๋ค๊ณ ๊ทธ ํจ์๋ฅผ ๋ฆฌํดํ๋ค.
function printHello() {
console.log('hello');
}
printHello();
2. parameters > function์ ์ ๋ฌ๋๋ ํ๋ผ๋ฏธํฐ
์๋ฐ์คํฌ๋ฆฝํธ๋ ํ์ ์ ๋ณด์ง ์์ ๋ฌธ์์ด์ด๋ ์ซ์์ด์ด๋ ๊ฐ์ด ์ถ๋ ฅํ๋ค.
ํ์ง๋ง ๋ค๋ฅธ์ธ์ด์์ ์ ํํ ํ์ ์ ์ ๋๊ฒ์ด ์ข๋ค.
function ์ผ๋ก log ํจ์๋ฅผ ๋ง๋ค์ด message ํ๋ผ๋ฏธํฐ๋ฅผ ํธ์ถ
// function log(message: string) > ํ์
์คํฌ๋ฆฝํธ ๋ฒ์
function log(message) {
console.log(message);
}
log('hello');
log(1234);
ํ๋ผ๋ฏธํฐ ์์ฉ
ํ๋ผ๋ฏธํฐ๋ ๊ฐ์ด ๋ฉ๋ชจ๋ฆฌ์ ์ ๋ฌ vs ์ค๋ธ์ ํธ ๋ฉ๋ชจ๋ฆฌ์ ๋ํผ๋ฐ์ค๊ฐ ์ ๋ฌ
function changeName(obj) {
obj.name = 'coder';
}
//์ ๋ฌ๋ ์ค๋ธ์ ํธ์ ์ด๋ฆ์ coder๋ก ๋ณ๊ฒฝํด๋ผ
const ellie = { name: 'ellie' }; // const๋ก ๋ณ์ ellie ๋ฅผ ์ ์
changeName(ellie); //๋ฆฌํด
console.log(ellie);
์ ์ธ๋ function ํจ์ changeName์ ํ๋ผ๋ฏธํฐ obj์ name ์
prototype ์ด object ์ด๋ค.
const ๋ก ellie๋ฅผ ์ ์ํด์ค > ์ด๋ ๋ฉ๋ชจ๋ฆฌ์ name์ค๋ธ์ ํธ์ ๋ํผ๋ฐ์ค์ธ ellie๊ฐ ์ ์ฅ๋จ
ํจ์์์์ ๋ํผ๋ฐ์ค๊ฐ ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ๋ณ๊ฒฝ๋ ๊ฐ์ด ์ถ๋ ฅ
3. Default parameters > ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์(ํ๋ผ๋ฏธํฐ)
default parameter์ ํจ์์ ์ ๋ฌ๋ ํ๋ผ๋ฏธํฐ์ ๊ฐ์ด undefined์ด๊ฑฐ๋ ์ ๋ฌ๋ ๊ฐ์ด ์์ ๋, ์ด๊ธฐํ ์ค์ ๋ ๊ฐ.
๊ฐ๋จํ ๋ฐฉ๋ฒ
function showMessage(message, from = 'unknown') {
console.log(`${message} by ${from}`);
}
showMessage('hi');
//hi by unknown ์ถ๋ ฅ
๋ง์ฝ ๋งค๊ฐ๋ณ์ from ์ ๊ฐ์ด ์๋ค๋ฉด unknown ์ผ๋ก ์ฒ๋ฆฌ
๋ณต์กํ ๋ฐฉ๋ฒ
//์์ ์ฝ๋๋ฅผ ํ์ด์ฐ๋ฉด ์ด๋ ๋ค.
function showMessage(message, from) {
if (from === undefined) {
from = 'unknown';
}
console.log(`${message} by ${from}`);
}
showMessage('hi');
4. resr parameters > ... ์ฌ์ฉ์ ๋ฐฐ์ดํํ๋ก ์ ํ
function printAll(...args) {
for (let i = 0; i < args.length; i++) {
console.log(args[i]);
}
//๊ฐ๋จํ๋ฐฉ๋ฒ
for (const arg of args) {
console.log(arg);
}
//๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ
args.forEach((arg) => console.log(arg));
}
printAll('park', 'hyo', 'jin');
//์ธ์ 3๊ฐ ์ ๋ฌ
//...args ๋ก ์ธ์๊ฐ park hyo jin ์ ๋ฐฐ์ด๊ฐ์ ๋งํผ ๋ฃจํ๋ก ์ถ๋ ฅ
5. Local scope > ๋ฐ์์๋ ์์ด ๋ณด์ด์ง ์๊ณ ์์์๋ง ๋ฐ์ ๋ณผ ์ ์๋ค.
let globalMessage = 'global'; //global variable ์ ์ญ๋ณ์
function printMessage() {
let message = '์ง์ญ๋ณ์';
console.log(message); // local variable ์ง์ญ๋ณ์
console.log(globalMessage); //๋ถ๋ชจ๋ณ์๋ ์์์ ํธ์ถ๊ฐ๋ฅ
function printAnother() {
console.log(message);
let childMessage = 'ํจ์์์ํจ์';
console.log(childMessage);
}
printAnother();
// console.log(childMessage); ๋ถ๋ชจ๋ณ์๋ ์์์์ ๋ถ๋ฌ์ฌ์ ์์ผ๋, ์์ ์์ฑ๋ ๋ณ์๋ ๋ฐ์์ ํธ์ถ ๋ชปํจํจ
}
printMessage();
6. Return a value
function sum(a, b) {
return a + b;
}
const result = sum(1, 2);
console.log(`sum: ${sum(1, 2)}`);
7. ๋นจ๋ฆฌ ๋ฆฌํดํ๊ณ ๋นจ๋ฆฌ ๋ซ๊ธฐ
function upgradeUser(user) {
if (user.point > 10) {
//์กฐ๊ฑด์ด ์๋์ ๋ค์ํจ์๋ก ์งํ > ํจ์๊ฐ ๋ธ๋ญ์์์ ์์ฑ๋๋ฉด ๊ฐ๋
์ฑ์ด ๋จ์ด์ง
}
function upgradeUser(user) {
if (user.point <= 10) {
return;
} // ์กฐ๊ฑด์ด ๋ง์ง์์๋ ๋ฆฌํด์ผ๋ก ํจ์ ์ข
๋ฃํ๊ณ ๋ง์๋ ๋ค์๋ก์ง ์คํ
}
}
๋๊ธ