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

위의 조건을 만족하는 객체를 일급 객체라 한다. 자바스크립트의 함수는 위의 조건을 모두 만족하기 때문에 일급 객체다.

 

// 함수는 무명의 리터럴로 생성할 수 있다.
// 함수는 변수에 저장할 수 있다.
// 런타임(할당 단계)에 함수 리터럴이 평가되어 함수 객체가 생성되고 변수에 할당된다.
const increase = function (num) {
	return ++num;
};

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

// 함수는 객체에 저장할 수 있다
const predicates = { increase, decrease };

// 함수의 매개변수에 전당할 수 있다
function makeCounter(predicate) {
	let num = 0;
    
    return function() {
    	num = predicate(num);
        return num;
    };
}

// 함수는 매개변수에게 함수를 전달할 수 있다.
const increaser = makeCounter(predicates.increase);

일급 객체로서 함수가 가지는 가장 큰 특징은 일반 객체와 같이 함수의 매개변수에 전달할 수 있으며, 함수의 반환값으로 사용할 수도 있다는 것이다. 이는 함수형 프로그래밍을 가능케 하는 자바스크립트의 장점이다.

 

 

함수 객체의 프로퍼티

함수는 객체이기 때문에 함수도 프로퍼티를 가질 수 있다. 

함수 객체의 프로퍼티

square 함수의 모든 프로퍼티의 프로퍼티 어트리뷰트를 Object.getOwnPropertyDescriptors 메서드로 확인해 보면 다음과 같다.

function square(number) {
	return number * number;
}

console.log(Object.getOwnPropertyDescriptors(square));

/*
	{
    	length: {value: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: null, writable: false, enumerable: false, configurable: false},
        prototype: {value: {....}, writable: true, enumerable: false, configurable: false}
    }
*/

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

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

 

length 프로퍼티

함수 객체의 length 프로퍼티는 함수를 정의할 때 선언한 매개변수의 개수를 가리킨다.

function foo() {}
console.log(foo.length); // 0

function bar(x) {
	return x;
}
console.log(bar.length); // 1

 

name 프로퍼티

함수 객체의 name 프로퍼티는 함수 이름을 나타낸다. 

// 기명 함수 표현식
var namedFunc = function foo() {};
console.log(namedFunc.name); // foo

// 익명 함수 표현식
var anonymousFunc = function() {};
console.log(anonymousFunc.name); // anonymousFunc

// 함수 선언문
function bar() {}
console.log(bar.name); // bar

 

__proto__ 접근자 프로퍼티

모든 객체는 [[Prototype]] 이라는 내부 슬롯을 갖는다. __proto__ 프로퍼티는 [[Prototype]] 내부 슬롯이 가리키는 프로토타입 객체에 접근하기 위해 사용하는 접근자 프로퍼티다.

const obj = { a: 1 };

console.log(obj.__proto__ === Object.prototype);

// 객체 리터럴 방식으로 생성한 객체는 프로토타입 객체인 Object.prototype의 프로퍼티를 상속받는다.
// hasOwnProperty 메서드는 Object.prototype의 메서드다.
console.log(obj.hasOwnProperty('a')); // true
console.log(obj.hasOwnProperty('__proto__')); // false

 

prototype 프로퍼티

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

일반 객체와 생성자 함수로 호출할 수 없는 non-constructor에는 prototype 프로퍼티가 없다.

// 함수 객체는 prototype 프로퍼티를 소유한다.
(function () {}).hasOwnProperty('prototype'); // true

// 일반 객체는 prototype 프로퍼티를 소유하지 않는다.
({}).hasOwnProperty('prototype'); // false

+ Recent posts