일급객체 (first-class)

lzlko·2022년 4월 15일
0

일급객체(First-class Object)
다른 객체들에 일반적으로 적용 가능한 연산을 모두 지원하는 객체
객체를 값으로 취급하는 것

1. 일급객체의 조건

1. 함수는 익명의 객체로 생성가능하다.

const increase = function (num) {
  return ++num;
};

const decrease = function (num) {
  return --num;
};

2. 함수는 객체,변수에 저장할 수 있다.

const test = { increase, decrease };

3. 함수는 매개변수에게 함수를 전달할 수 있다.

const increaser = makeCounter(test.increase);
console.log(increaser()); // 1
console.log(increaser()); // 2

4. 다른 함수의 결과로서 리턴될 수 있다.

function makeCounter(test) {
  let num = 0;

  return function () {
    num = test(num);
    return num;
  };
}

함수는 일급객체의 조건에 해당한다.

2. 일급객체가 할 수 있는 것

  1. 고차 함수(forEach, filter, map, sort 등)
  2. closer
  3. 콜백 패턴

0개의 댓글