JavaScript callback

LemonΒ·2022λ…„ 12μ›” 21일
0

JavaScript

λͺ©λ‘ 보기
17/17
post-thumbnail

πŸ’‘ ν•΄λ‹Ή ν¬μŠ€νŒ…μ€ μƒν™œμ½”λ”©λ‹˜μ˜ callback μˆ˜μ—…μ„ μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€.
μƒν™œμ½”λ”© - JavaScript callback μˆ˜μ—… λ“€μœΌλŸ¬κ°€κΈ°

ν•¨μˆ˜(callbackFunction)
λ‹€λ₯Έ ν•¨μˆ˜μ˜ μž…λ ₯ν•¨μˆ˜μ— 전달 λ˜μ–΄μ„œ λ‹€λ₯Έ ν•¨μˆ˜μ— μ˜ν•΄μ„œ λ‚˜μ€‘μ— ν˜ΈμΆœλœλ‹€κ³  ν•΄μ„œ CALLBACK FUNCTION이라고 λΆ€λ₯Έλ‹€.


first class citizen

일급 μ‹œλ―Ό ν˜Ήμ€ 일급 객체

일급 μ‹œλ―Ό ν•΄λ‹Ή 문법

val = 1 

1은 λ³€μˆ˜μ˜ 값이 될 수 μžˆλ‚˜μš”? Yes β†’ 일급 μ‹œλ―Ό


val = if(πŸ‘) {🐎};

쑰건문은 λ³€μˆ˜μ˜ 값이 될 수 μžˆλ‚˜μš”? No β†’ 이급 μ‹œλ―Ό(Second calss citizen)


val = function(πŸ‘){ return 🐎 }

ν•¨μˆ˜λŠ” λ³€μˆ˜μ˜ 값이 될 수 μžˆλ‚˜μš”? Yes β†’ 일급 μ‹œλ―Ό


일급 μ‹œλ―Όμ΄ 되기 μœ„ν•œ 또 ν•˜λ‚˜μ˜ 쑰건

function fn(){
	val = function(πŸ‘){ return 🐎 }
	return val;
}

fnμ΄λΌλŠ” ν•¨μˆ˜λŠ” val을 λ¦¬ν„΄ν•œλ‹€.
ν•¨μˆ˜κ°€ λ‹€λ₯Έ ν•¨μˆ˜μ˜ return 값이 될 수 μžˆλ‹€λ©΄ κ·Έ μ–Έμ–΄λŠ” ν•¨μˆ˜λ₯Ό 일급 μ‹œλ―ΌμœΌλ‘œ λŒ€μš°ν•΄μ€€λ‹€.


val = function(πŸ‘){ return 🐎 }
fn(val)

valμ΄λΌλŠ” ν•¨μˆ˜λŠ” fnμ΄λΌλŠ” ν•¨μˆ˜μ˜ μž…λ ₯κ°’μœΌλ‘œ μ‚¬μš©λ˜κ³  μžˆλ‹€.
ν•¨μˆ˜κ°€ λ‹€λ₯Έ ν•¨μˆ˜μ˜ μž…λ ₯값이 될 수 μžˆλ‹€λ©΄ κ·Έ μ–Έμ–΄μ—μ„œλŠ” ν•¨μˆ˜λ₯Ό 일급 μ‹œλ―ΌμœΌλ‘œ λŒ€μš°ν•΄μ£Όκ³ μžˆλ‹€λŠ” 것이닀.


콜백 ν•¨μˆ˜λž€?

fnμ΄λΌλŠ” ν•¨μˆ˜λŠ” argλΌλŠ” νŒŒλΌλ―Έν„°λ₯Ό λ°›μ•„μ„œ fn ν•¨μˆ˜ μ•ˆμ—μ„œ argλ₯Ό ν•¨μˆ˜λ‘œμ„œ ν˜ΈμΆœν•˜κ³ μžˆλ‹€.
val은 μ§€κΈˆ λ‹Ήμž₯ ν˜ΈμΆœλ˜μ§„ μ•Šμ§€λ§Œ λ‹€λ₯Έ ν•¨μˆ˜μ˜ μž…λ ₯ν•¨μˆ˜μ— 전달 λ˜μ–΄μ„œ λ‹€λ₯Έ ν•¨μˆ˜μ— μ˜ν•΄μ„œ λ‚˜μ€‘μ— ν˜ΈμΆœλœλ‹€κ³  ν•΄μ„œ callback function이라고 λΆ€λ₯Έλ‹€.
즉, val μžμ²΄λŠ” μ½œλ°±ν•¨μˆ˜κ°€ μ•„λ‹ˆμ§€λ§Œ val ν•¨μˆ˜κ°€ λ‹€λ₯Έ ν•¨μˆ˜μ˜ μž…λ ₯κ°’μœΌλ‘œ μ „λ‹¬λΌμ„œ 그것이 ν˜ΈμΆœλœλ‹€κ³  ν•˜λ©΄ callback function이 λ˜λŠ” 것이닀.

콜백 ν•¨μˆ˜μ˜ 사둀

Array.prototype.filter() - JavaScript | MDN

const words = ["spray", "limit", "elite", "exuberant", "destruction", "present"];
const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

word β‡’ word.length>6 ν•¨μˆ˜λ₯Ό filterλΌλŠ” ν•¨μˆ˜μ˜ μž…λ ₯κ°’μœΌλ‘œ μ£Όμž…μ‹œμΌœμ€¬λ‹€.
μ΄λ•Œ word β‡’ word.length>6 κ°€ callback ν•¨μˆ˜μ΄λ‹€.


ꡬ문(=문법)

arr.filter(**callback(element[, index[, array]]**)[, thisArg])
  • filterν•¨μˆ˜λŠ” 첫번째 νŒŒλΌλ―Έν„°λ‘œ ν•¨μˆ˜(callback ν•¨μˆ˜)λ₯Ό 받도둝 λ˜μ–΄μž‡λ‹€.
  • κ·Έ ν•¨μˆ˜λŠ” 3개의 νŒŒλΌλ―Έν„°λ₯Ό κ°€μ§ˆ 수 μžˆλ‹€.
    • elementλŠ” ν•„μˆ˜, λ‚˜λ¨Έμ§€λŠ” μ˜΅μ…˜
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

function callback(element) {
  console.log(element); 
	// 'spray' 'limit' 'elite' 'exuberant' 'destruction' 'present'
	// μ½œλ°±ν•¨μˆ˜λŠ” 첫번째 νŒŒλΌλ―Έν„°λ‘œ 각각의 μ›μ†Œλ₯Ό 쀄 것이라고 μ•½μ†λ˜μ–΄μžˆλ‹€.

  if(element.length > 6){
    return true
  } else {
    return false
  }
}

newWords = words.filter(callback) 
console.log(newWords) // [ 'exuberant', 'destruction', 'present' ]

μ½”λ“œ λ‹¨μˆœν™”

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

function callback(element) {
	return element.length > 6
}

newWords = words.filter(callback) 
console.log(newWords) // [ 'exuberant', 'destruction', 'present' ]

일반적으둜 μ½œλ°±ν•¨μˆ˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄ μ“°λŠ” κ²½μš°λ„ μžˆμ§€λ§Œ, μ•„λ‹Œ κ²½μš°μ— λ”± ν•œλ²ˆλ§Œ μ‚¬μš©ν•˜λŠ” κ²½μš°κ°€ λ§Žλ‹€.
κ·Έλž˜μ„œ μ½œλ°±ν•¨μˆ˜μ™€ μ½œλ°±μ„ μ†ŒλΉ„ν•˜λŠ” ν•¨μˆ˜κ°€ 멀리 λ–¨μ–΄μ ΈμžˆμœΌλ©΄ μ„œλ‘œ 응집λ ₯이 떨어진닀.
μ΄λŸ΄λ•Œ 많이 μ“°λŠ”κ²Œ 이름을 λΊλŠ” 것이닀. ( = 읡λͺ…ν•¨μˆ˜ )
이 읡λͺ…ν•¨μˆ˜λ₯Ό μ½œλ°±μžλ¦¬μ— λΆ™μ—¬λ„£κΈ°ν•΄μ„œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€. ( β‡’ 많이 μ“°λŠ” ν…Œν¬λ‹‰ )

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

newWords = words.filter(function(element) {
		return element.length > 6
	};
) // [ 'exuberant', 'destruction', 'present' ]

μ΅œκ·Όμ—λŠ” νƒ€μ΄ν•‘ν•˜κΈ° 싫은 κ°œλ°œμžλ“€μ΄ ν™”μ‚΄ν‘œ ν•¨μˆ˜λ₯Ό λ„μž…ν•΄μ„œ function도 μƒλž΅ν•΄μ„œ μ‚¬μš© κ°€λŠ₯

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

newWords = words.filter((element) => {return element.length > 6};) // [ 'exuberant', 'destruction', 'present' ]

그리고 νŒŒλΌλ―Έν„°κ°€ ν•˜λ‚˜μΈ κ²½μš°μ— ()도 ν•„μš”μ—†λ‹€.
ν•¨μˆ˜ μ½”λ“œκ°€ ν•œμ€„μΈ 경우 {}와 return 도 ν•„μš”μ—†λ‹€.

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

newWords = words.filter(element => element.length > 6) // [ 'exuberant', 'destruction', 'present' ]

μ΄λ ‡κ²Œ ν•˜λ©΄ μ•„μ£Ό κ°„κ²°ν•œ μ½”λ“œκ°€ λœλ‹€.
μ΄λ ‡κ²Œ λ§Œλ“€μ–΄μ§„ μ½”λ“œκ°€ λͺ¨μ§ˆλΌ 예제 μ½”λ“œμ΄λ‹€. https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

filterλΌλŠ” ν•¨μˆ˜λŠ” μš°λ¦¬κ°€ κ³΅κΈ‰ν•œ callback ν•¨μˆ˜λ₯Ό μ†ŒλΉ„ν•œλ‹€.


filterλ₯Ό 직접 λ§Œλ“€μ–΄λ³΄μž

직접 callback ν•¨μˆ˜λ₯Ό μ†ŒλΉ„ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄λ³΄μž

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
// newWords = words.filter(element => element.length > 6)
function myfilter(origin, callback){
	var result = [];
	for(var i=0; i<origin.length; i++){ // origin 데이터 μ‘°νšŒν•˜κΈ°
		var current = origin[i] // ν˜„μž¬ μˆœλ²ˆμ„ current에 λ‹΄κΈ°
		if(callback(current)){ // currentλ₯Ό callbackν•¨μˆ˜μ˜ μž…λ ₯κ°’μœΌλ‘œ μ „λ‹¬ν–ˆμ„ λ•Œ true 라면
			result.push(current); // 빈 배열인 result λ³€μˆ˜μ— current의 값을 μΆ”κ°€ν•œλ‹€. 
		}
	}
	return result; // λ§Œλ“€μ–΄μ§„ μƒˆλ‘œμš΄ result 배열을 return ν•œλ‹€. 
}
newWords = myfilter(words, element => element.length > 6);
console.log(newWords); // [ 'exuberant', 'destruction', 'present' ]

filter 예제 μ½”λ“œμ™€ λ™μΌν•œ λ™μž‘μ„ ν•˜λŠ” myfilterλΌλŠ” ν•¨μˆ˜κ°€ λ§Œλ“€μ–΄μ‘Œλ‹€.

마무리

filter ν•¨μˆ˜λ₯Ό 톡해 callback ν•¨μˆ˜κ°€ 무엇인지, μ–΄λ–»κ²Œ ν™œμš©ν•˜λŠ”μ§€μ— λŒ€ν•΄ μ•Œμ•„λ³΄μ•˜λ‹€.
callback ν•¨μˆ˜λŠ” λ‹€λ₯Έ ν•¨μˆ˜μ˜ μž…λ ₯ν•¨μˆ˜μ— 전달 λ˜μ–΄μ„œ λ‹€λ₯Έ ν•¨μˆ˜μ— μ˜ν•΄μ„œ λ‚˜μ€‘μ— ν˜ΈμΆœλ˜λŠ” ν•¨μˆ˜μ΄λ‹€.

profile
ν”„λ‘ νŠΈμ—”λ“œ 개발자 κ°€λ³΄μžκ³ ~!!

0개의 λŒ“κΈ€