๐Ÿฆœ Computer Language/JavaScript

[JavaScript] ์ฝœ๋ฐฑ ํ•จ์ˆ˜

function checkList(score, good, bad){
	score > 95 ? good() : bad();
}

function pass(){
	console.log("PASS์ž…๋‹ˆ๋‹ค.");
}

function fail(){
	console.log("FAIL์ž…๋‹ˆ๋‹ค.");
}

checkList(96, pass, fail);

์ฝœ๋ฐฑํ•จ์ˆ˜: ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํ•จ์ˆ˜๋ฅผ ๋„˜๊ธฐ๋Š” ๊ฒƒ

 

checkList ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ score ๊ฐ’์— ๋”ฐ๋ผ

pass์™€ failํ•จ์ˆ˜ ์ค‘ ๋‘˜ ์ค‘ ํ•œ๊ฐ€์ง€๊ฐ€ ๋‚˜์ค‘์— ํ˜ธ์ถœ๋œ๋‹ค.

 

 

  • ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ
function add(x, y){
	return x + y;
}

function getResult(result){
	console.log(result);
}

getResult(add(4,5));โ€‹

 

  • ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ
function add(x, y, callback){
	callback(x + y);
}

function getResult(result){
	console.log(result);
}

add(4, 5, getResult);