const o = {};
o.[[Prototype]] // -> Uncaught SyntaxError : Unexpected token '['
o.__proto__ // -> Object.prototype
자바스크립트 엔진은 프로퍼티를 생성할 때 프로퍼티 상태를 나타내는 프로퍼티 어트리뷰트를 기본값으로 자동 정의한다.
[[Value]] : 프로퍼티의 값
[[Writable]] : 값의 갱신 가능 여부
[[Enumerable]] : 열거 가능 여부
[[Configurable]] : 재정의 가능 여부
Object.getOwnPropertyDescriptor 메서드를 통해 하나의 프로티에 대한 프로퍼티 디스크립터 객체를 반환.
Object.getOwnPropertyDescriptors 메서드를 통해 모든 프로퍼티에 대한 프로퍼티 디스크립터 객체를 반환.
const person = {
name: 'Lee'
};
console.log(Object.getOwnPropertyDescriptor(person, 'name'));
// {value: 'Lee', writable: true, enumerable: true, configurable: true}
키와 값으로 구성된 일반적인 프로퍼티
== getter/setter 함수
다른 데이터 프로퍼티의 값을 읽거나 저장할 때 호출되는 접근자 함수로 구성된 프로퍼티, 자체적으로 값을 가지진 않음
// 일반 객체의 __proto__는 접근자 프로퍼티다
Object.getOwnPropertyDescriptor(Object.prototype, '__proto__');
// {get: f, set: f, enumerable: false, configurable: true}
// 함수 객체 prototype은 데이터 프로퍼티다
Object.getOwnPropertyDescriptor(function() {}, 'prototype');
// {value: {...}, writable: true, enumerable: false, configurable: false}
새로운 프로퍼티를 추가하면서 프로퍼티 어트리뷰트를 명시적으로 정의하거나, 기존 프로퍼티의 프로퍼티 어트리뷰트를 재정의하는 것
Object.defineProperty메서드 사용
인수로 객체의 참조, 데이터 프로퍼티 키, 프로퍼티 디스크립터 객체를 전달
const person = {};
// 데이터 프로퍼티 정의
Object.defineProperty(person, 'firstName', {
value: 'Ungmo',
wrirable: true,
enumerable: true,
configurable: true
});
Object.defineProperty(person, 'lastName', {
value: 'Lee'
});
// 접근자 프로퍼티 정의
Object.defineProperty(person, 'fullName', {
// getter 함수
get() {
return `${this.firstName} ${this.lastName}`;
}
// setter 함수
set(name) {
[this.firstName, this.lastName] = name.split(' ');
},
enumerable: true,
configurable: true
});
자바스크립트가 객체 변경을 방지하기 위한 다양한 메서드를 제공한다.
객체의 중첩 객체까지 동결하여 불변 객체를 구현하려면 재귀적으로 Object.freeze메서드를 호출해야 함