일급 객체 조건

다음과 같은 조건을 만족하는 객체를 일급 객체라고 한다.

  1. 무명의 리터럴로 생성할 수 있습니다. 즉, 런타임에 생성이 가능하다.
  2. 변수나 자료구조(객체, 배열 등)에 저장할 수 있다.
  3. 함수의 매개변수에 전달할 수 있다.
  4. 함수의 반환값으로 사용할 수 있다.

자바스크립트의 함수는 다음 예제와 같이 위의 조건을 모두 만족하므로 일급 객체다.

일급 객체라는 것은 함수를 객체와 동일하게 사용할 수 있다는 의미다.

함수 객체의 프로퍼티

function square(number) (
return number number;

console.log(Object.getOwnPropertyDescriptors(square));
/*
    Length: {νalue: 1, writable: false, enumerable: false, configurable: true},
    name: {value: "square", writable: false, enumerable: false, configurable: true},
    arguments: {value: null, writable: false, enumerable: false, configurable: false},
    caller: (value: noll, aritable: false, enumerable: false, configurable: false},
    protarype: (value: {...}, writables true, enumerable: false, configurable: false}
*/

// __proto__는 square의 함수가 아니다.
console.log(Object.getOwnPropertyDescriptor(square, '__prote__')); // undefined

// __proto__는 Object.prototype 객체의 접근자 프로퍼티다.
// square 함수는 Object.prototype 객체로부터 __proto__ 접근자 프로퍼티를 상속받는다.
console.log(Object.getOwnPropertyDescriptor(Object.prototype,'__prato__'));
// {get: f set: f, enumerables false, configurable: true)

arguments 프로퍼티

함수 객체의 arguments 프로퍼티 값은 arguments 객체다. arguments 객체는 함수 호출 시 전달된 인수(argument)들의 정보를 담고 있는 순회 가능한(iterable) 유사 배열 객체이며, 함수 내부에서 지역 변수처럼 사용된다. 즉, 함수 외부에서는 참조할 수 없다.

프로퍼티 키는 인수의 순서를 나타낸다.

arguments 객체는 유사배열 객체다.

prototype 프로퍼티

__proto__와 다르게, prototype 프로퍼티는 생성자 함수로 호출할 수 있는 함수 객체, 즉 constructor만이 소유하는 프로퍼티다.

prototype 프로퍼티는 함수가 객체를 생성하는 생성자 함수로 호출될때 생성자 함수가 생성할 인스턴스의 프로토타입 객체를 가리킨다.