์ฝ๋ฐฑํจ์
-> ๋์ค์ ๋ค์ ๋ถ๋ ค์ง๋ ํจ์
-> ํน์ ์ด๋ฒคํธ์ ์ํด ํธ์ถ๋๋ ํจ์
-> ๋ค๋ฅธ ํจ์์ ์ธ์๋ก์จ ๋๊ฒจ์ฃผ๋ ์คํ๊ฐ๋ฅํ ์ฝ๋
function div(x, y) {
return x / y;
}
function calculator(callback, x, y) {
return callback(x, y);
}
console.log(calculator(div, 7, 3));
// 2.3333333333333335
function ask(question, yes, no) {
if (confirm(question)) yes()
else no();
}
function showOk() {
alert( "๋์" );
}
function showCancel() {
alert( "์ทจ์" );
}
ask("๋์", showOk, showCancel);
// ํจ์ showOk์ showCancel๊ฐ ask ํจ์์ ์ธ์๋ก ์ ๋ฌ๋จ
์ฌ๊ธฐ์ ๋ณด๋ฉด ๋ ์ดํด๊ฐ ์ ๋๋๋ฐ, ํ๋ง๋๋ก ์ฝ๋ฐฑํจ์๋ ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋๊ณ ๊ฐ์ ธ๋ค ์ด๋ค๊ณ ์ดํดํ๋ฉด ํธํ๋ค. ์ฌ๊ธฐ์๋ showOk, showCancel๋ฅผ ์ฝ๋ฐฑํจ์๋ผ๊ณ ํ๋ค.
์ฆ, ํจ์๋ฅผ ํจ์์์ธ์๋ก ์ ๋ฌํ๊ณ ์ธ์๋ก ์ ๋ฌํ ํจ์๋ฅผ ๋์ค์ ํธ์ถ (callback)ํ๋ ๊ฒ์ด ์ฝ๋ฐฑํจ์์ ๊ฐ๋ ์ด๋ค.
์์์๋ yes() ์๋ฆฌ์ showOk ํจ์๊ฐ ๋ค์ด๊ฐ๊ณ , no() ์๋ฆฌ์ showCancel ํจ์๊ฐ ๋ค์ด๊ฐ๋ค.