ECMAScript 사양에 등장하는 이중 대괄호 [[...]]
로 감싼 이름들이 내부 슬롯과 내부 메서드다.
JS 엔진에서 동작하지만, 개발자가 직접 접근할 수 있도록 공개된 객체의 프로퍼티는 아니다.
모든 객체는 [[Prototype]]
이라는 내부 슬롯을 갖는다. 원칙적으로 접근할 수 없지만, 내부 슬롯의 경우 __proto__
를 통해 간접 접근할 수 있다.
const o = {};
// 내부 슬롯은 자바스크립트 엔진에 내부 로직이므로 접근할 수 없다.
o.[[Prototype]] // -> Uncaught SyntaxError: Unexpected token '['
// 단, 일부 내부 슬롯과 내부 메서드에 한하여 간접적으로 접근할 수 있는 수단이 있다.
o.__proto__ // -> Object.prototype
<aside> 💡 프로퍼티 어트리뷰트 : 프로퍼티의 상태 프로퍼티 상태 : 프로퍼티 값, 값의 갱신 가능 여부, 열거 가능 여부, 재정의 가능 여부
</aside>
프로퍼티 어트리뷰트는 JS 엔진이 관리하는 내부 상태 값인 내부 슬롯 [[Value]]
, [[Writable]]
, [[Enumerable]]
, [[Configurable]]
이다.
어트리뷰트에 직접 접근할 수 없지만, Object.getOwnPropertyDescriptor
메서드로 간접 확인할 수 있다.
이 메서드는 프로퍼티 디스크립터 객체를 반환한다.
const pseson = {
name: 'Lee'
};
// 프로퍼티 어트리뷰트 정보를 제공하는 프로퍼티 디스크립터 객체를 반환한다.
console.log(Object.getOwnPropertyDescriptor(person, 'name'));
// {value: "Lee", writable: true, enumerable: true, configurable: true}
<aside> 💡 데이터 프로퍼티 : 키와 값으로 구성된 일반적인 프로퍼티 접근자 프로퍼티 : 자체적으로 값을 갖지 않고 다른 데이터 프로퍼티의 값을 읽거나 저장할 때 호출되는 접근자 함수로 구성된 프로퍼티
</aside>
const person = {
// data property
firstName: 'Ungmo',
lastName: 'Lee',
// fullName은 접근자 함수로 구성된 접근자 프로퍼티다.
// getter 함수
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
// setter 함수
set fullName(name) {
[this.firstName, this.lastName] = name.split(' ');
}
};
// 데이터 프로퍼티를 통한 프로퍼티 값의 참조
console.log(person.firstName + ' ' + person.lastName); // Ungmo Lee
// 접근자 프로퍼티를 통한 프로퍼티 값의 저장
// 접근자 프로퍼티 fullName에 값을 저장하면 setter함수가 호출된다.
person.fullName = 'Heegun lee';
console.log(person); // {firstName: 'Heegun', lastName: 'lee'}
//접근자 프로퍼티를 통한 프로퍼티 값의 참조
// 접근자 프로퍼티 fullName에 접근하면 getter 함수가 호출된다.
console.log(person.fullName); // Heegun Lee
// firstName은 데이터 프로퍼티다.
// 데이터 프로퍼티는 [[value]], [[Writable]], [[Enumerable]], [[Configurable]]
// 프로퍼티 어트리뷰트를 갖는다.
let descriptor = Object.getOwnPropertyDescriptor(person, 'firstName');
console.log(descriptor);
// [value: 'Heegun', writable: true, enumerable: true, configurable: true}
// fullName은 접근자 프로퍼티다.
// 접근자 프로퍼티는 [[Get]], [[Set]], [[Enumerable]], [[Configurable]]
// 프로퍼티 어트리뷰트를 갖는다.
descriptor = Object.getOwnPropertyDescriptor(person, 'fullName');
console.log(descriptor);
// {get: f, set: f, enumerable: true, configurable: true}