일급객체(First-class Object)
다른 객체들에 일반적으로 적용 가능한 연산을 모두 지원하는 객체
객체를 값으로 취급하는 것
const increase = function (num) {
return ++num;
};
const decrease = function (num) {
return --num;
};
const test = { increase, decrease };
const increaser = makeCounter(test.increase);
console.log(increaser()); // 1
console.log(increaser()); // 2
function makeCounter(test) {
let num = 0;
return function () {
num = test(num);
return num;
};
}
함수는 일급객체의 조건에 해당한다.
- 고차 함수(forEach, filter, map, sort 등)
- closer
- 콜백 패턴