์ฝ๋ฐฑ (Call back) ํจ์
๊ธฐ๋ณธ ์ ์ผ๋ก ํจ์๊ฐ ๋ค๋ฅธ ํจ์์ ์ธ์๋ก ๋ค์ด ๊ฐ ์ ์๋ ํํ์ด๋ค. ์ด๋ค ํจ์๋ฅผ ์คํํ๊ธฐ ์ด์ ๋จผ์ ์ํํ ๊ฒ์ ํ๊ณ ๋ค์ ์๊ธฐ๊ฒ์ผ๋ก ๋์์ ๋ง์ ์งํํ๋ค.
์ด๋ฅผ ๋น๋๊ธฐ์ ๋ฐฉ์์ด๋ผ๊ณ ํ๋ค. ์์ฐจ์ ์ผ๋ก ์ผ์ด๋์ง ์๊ณ ๋ค๋ฅธ ์์๋๋ก ์งํํ๋ ๊ฒ์ ์๋ฏธํ๋ค.
//์ฝ๋ฐฑ ํจ์ - ๊ธฐ๋ณธ
// Hello jiho ๋ฅผ ์ถ๋ ฅํ๋ ํจ์ greeting
function greeting(name) {
console.log(`Hello ${name}`);
}
//greeting์ ํ์ดํ ํจ์๋ก ๋ณ๊ฒฝ
//const greeting = () => console.log(`Hello ${name}`);
//greeting('jiho');
function processUserInput(name, callbackFunction) {
callbackFunction(name);
}
processUserInput('jiho', greeting);
// ํจ์ 2๊ฐ ๊ตฌํ
// 1. ์ฟ ํก์์ ์ฌ๊ณผ๋ฅผ ๊ธฐ๋ค๋ฆฌ๋ ํจ์ waitCoupang(appleBox, callback)
// ํจ์์ ๋์: '์ฟ ํก์์ ${appleBox}๊ฐ ๋์ฐฉํ๋ค'๋ฅผ ์ถ๋ ฅํ๋ ํจ์
// 2. bringItToNeighbor(๋ณ๋์ ํ๋ผ๋ฏธํฐ ์์)
// ํจ์์ ๋์: '์์ง ์์ฃผ๋จธ๋์๊ฒ ์ ๋ฌ ์๋ฃ'๋ฅผ ์ถ๋ ฅํ๋ ํจ์
function bringItToNeighbor() {
console.log("์์ง ์์ฃผ๋จธ๋์๊ฒ ์ ๋ฌ ์๋ฃ");
}
function waitCoupang(appleBox, callback) {
console.log(`์ฟ ํก์์ ${appleBox}๊ฐ ๋์ฐฉํ๋ค.`);
callback();
}
waitCoupang('์ฌ๊ณผ', bringItToNeighbor);
const callBack = number => console.log(number);
numbers.forEach(callBack);
๋๊ธฐ์ ๋ฐฉ์ : ์์ฐจ์ ์ผ๋ก ์งํ๋๋ ๊ฒ์ ์๋ฏธ
๋น๋๊ธฐ์ ๋ฐฉ์ : ์์ฐจ์ ์ผ๋ก ์งํ ๋์ง ์๊ณ ํน์ ํ ๊ฒ์ ๋จผ์ ๋๋ด๊ณ ๊ทธ ๋ค์ ์งํํ์ฌ ์์ฐจ์ ์ผ๋ก ์งํ๋์ง ์์ ๊ฒ์ ์๋ฏธ
'๊ฐ๋ฐ ์ง์, ์คํฌ๋ฆฝํธ > JavaScript, TS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ชจ๋(module)์ด๋ ? (์์ฑํ์) (0) | 2023.11.27 |
---|---|
์ ์ญ ๋ณ์์ ์ง์ญ ๋ณ์ ๊ทธ๋ฆฌ๊ณ scope (0) | 2023.11.27 |
ํจ์ ํํ์ (0) | 2023.11.27 |
์๋ฐ ์คํฌ๋ฆฝํธ ํจ์ (0) | 2023.11.27 |
forEach ๊ตฌ๋ฌธ (0) | 2023.11.27 |
๋๊ธ