6.2 호출과 참조

  • 자바스크립트에서는 함수도 객체 --> 다른 객체와 마찬가지로 넘기거나 할당이 가능

  • 함수 식별자 뒤에 괄호를 써서 함수 호출 (쓰지 않으면 단순 참조)

  • 함수를 변수, 객체 프로퍼티, 배열 요소로 할당 가능

1) 함수를 변수에 할당하는 경우

  var f = getGreeting;
  console.log(f()); // Hello, World!
  console.log(f); // getGreeting() { return "Hello, World!"; }

2) 함수를 객체 프로퍼티에 할당하는 경우

  const o = {};
  o.f = getGreeting;
  console.log(o.f()); // Hello, World!
  console.log(o.f); // getGreeting() { return "Hello, World!"; }

3) 함수를 배열 요소에 할당하는 경우

  const arr = [1, 2, 3];
  arr[1] = getGreeting; // arr[ 1, function getGreeting(), 2 ]
  console.log(arr[1]()); // Hello, World!
  console.log(arr[1]); // getGreeting() { return "Hello, World!"; }

Last updated