고차 함수 (Higher order function)

uoah·2023년 2월 7일
0

기초 튼튼

목록 보기
7/15
post-thumbnail

📍 고차 함수

함수를 인자로 받거나 결과로 반환하는 함수

✅ 예)

const func = () => {
    return () => {
        console.log('hello');
    }
}
const innerFunc = func();
innerFunc(); 	// hello

이해가 어렵다면, func() 부분을 반환한 값으로 대체해 보자.

const innerFunc = () => {
	console.log('hello')';
};
innerFunc(); 	// hello

✅ 매개 변수가 있을 때)

const innerFunc = (msg) => {
	console.log(msg);
};
innerFunc(); 	// hello
const innerFunc1 = func('hello');
const innerFunc2 = func('javascript');
const innerFunc3 = func();

innerFunc1();	// hello
innerFunc2();	// javascript
innerFunc3();	// undefined

대체를 해 보자.

const innerFunc1 = () => {
	console.log('hello');
};

const innerFunc2 = () => {
	console.log('javascript');
}

const innerFunc3 = () => {
	console.log(); // 빈 값은 undefined
}

함수가 중복되면 고차 함수를 사용하면 된다.

0개의 댓글