1. 메모이제이션(Memoization)

메모이제이션이란 뭘까요? 메모이제이션이 무엇인지 알아보고 다음 코드가 어떻게 동작하는지 분석해봅시다.

function factorial(n) {
  if (Number.isInteger(n) && n > 0) {
    if (!(n in factorial)) {
      factorial[n] = n * factorial(n - 1);
    }
    return factorial[n];
  } else {
    return '잘못된 숫자가 입력되었습니다.';
  }
}

factorial[1] = 1; // 캐시를 초기화합니다.
factorial(6); // => 720
factorial[5]; // => 120 / 이 값은 이미 캐시에 존재합니다.

<aside> 💡 메모이제이션은 리액트의 Hooks를 통해 직접적으로 사용됩니다!

</aside>

2. 클로저-중첩-함수

클로저에 대해 이해하려면 먼저 어휘적 스코프(lexical scope)에 대해 알아야한다.

어휘적 스코프는 선언된 환경이 무엇인가에 대해 말하는 것이다.

우리가 이전에 공부한 Scope의 대한 개념으로 코드를 함께보자

const myName = 'YJOO';

function getName() {
  return myName;
}
//myName Global Scope

위 코드의 어휘적 스코프는 어디일까?

myName이 전역 환경에 선언되었으니 어휘적 스코프는 Global Scope다

function getName() {
  const myName = "DragonCastle";
  return myName;
}
//myName Function Local Scope

function showLastName() {
  const lastName = 'Sofela';
  return lastName; 
}
//lastName showLastName의 Function Local Scope
//showLastName은 Global Scope
function displayFullName() {
  const fullName = 'Oluwatobi ' + lastName;
  return fullName;
}
//displayFullName도 Global Scope
console.log(displayFullName()); // => ReferenceError: lastName is not defined
lastName displayFullName showLastName
어휘적 스코프 showLastName의 Function Local Scope Global Scope Global Scope

이렇게 알아본 내용을 기반으로 클로저에 대해 정리해보자

클로저 - JavaScript | MDN

3. 프로토타입

자바스크립트에는 클래스라는 개념이 없다.

대신 기존의 객체를 복사하여 새로운 객체를 생성하는 기법인 프로토타입이 있다.