[JS] (16) 프로퍼티 어트리뷰트 | 자바스크립트 Deep Dive

sehannnnnnn·2021년 8월 5일
0
post-thumbnail

내부 슬롯과 내부 메서드

내부 슬롯 (Internal slot) 과 내부 메소드 (Internal method)는 자바스크립트 엔진의 구현 알고리즘을 설명하기 위해 ECMAScript 사양에서 사용하는 의사 프로퍼티(pseudo property)와 의사 메서드(pseudo method)이다.

https://262.ecma-international.org/11.0/#sec-property-attributes
6.1.7.2 Object Internal Method and Internal Slot

위 사이트를 통해 이중 괄호로 감싸진 내부 슬롯, 메소드를 확인할 수 있다. 내부 슬롯, 메서드는 실제로는 동작하지만, 개발자가 직접 접근하는 것은 불가능하다. (내부에서 작동하는 로직이기 때문) 근데 일부만 간접적으로 접근할 수 있는 수단을 제공한다.

const o = {};

o.[[Prototype]] //SyntaxError

o.__proto__ // Object.prototype

프로퍼티 어트리뷰트와 프로퍼티 디스크립터 객체

자바스크립트는 프로퍼티를 생성하면 프로퍼티의 상태를 나타내는 프로퍼티 어트리뷰트라는 것을 기본값으로 자동 정의한다. 프로퍼티 어트리뷰트에는 4가지가 있는데

  • [[Value]] : 프로퍼티의 값 - Undefined
  • [[Writable]] : 값의 갱신가능 여부 - Boolean
  • [[Enumerable]] : 열거 가능 여부 - Boolean
  • [[Configurable]] : 재정의 가능 여부 - Boolean

이에 대한 값은 Object.getOwnPropertyDescriptor 메서드 로 간접적으로 확인 할 수 있다.

const person = {
	name : 'Kim'
}

console.log(Object.getOwnPropertyDescriptor(person, 'name')
//{value: 'Lee', writable: true, enumerable: true, configurable: true}

이 메서드를 활용하면 프로퍼티 어트리뷰트에 대한 정보를 담은 하나의 객체를 반환해주는데 이를 프로퍼티 디스크립터 객체라 한다.

const person = {
	name : 'Kim'
}

person.age = 20;

console.log(Object.getOWnPropertyDescriptors(person);
// person에 대한 프로퍼티 디스크립터 객체 반환

데이터 프로퍼티와 접근자 프로퍼티

  • 데이터 프로퍼티 (data property)
    키와 값으로 구성된 일반적인 프로퍼티다. 지금까지 살펴본 모든 프로퍼티는 데이터 프로퍼티다.
  • 접근자 프로퍼티 (accessor property)
    자체적으로는 값을 갖지 않고 다른 데이터 프로퍼티의 값을 읽거나 저장할 때 호출되는 접근자 함수로 구성된 프로퍼티다.

데이터 프로퍼티

데이터 프로퍼티의 어트리뷰트는 다음과 같은 값을 가진다.

  • [[Value]] : 프로퍼티의 값 - Undefined
  • [[Writable]] : 값의 갱신가능 여부 - Boolean (초기값 true)
  • [[Enumerable]] : 열거 가능 여부 - Boolean (초기값 true)
  • [[Configurable]] : 재정의 가능 여부 - Boolean (초기값 true)

위에서 말했던 것과 같다!
[[Value]]는 프로퍼티 값에 접근하면 반환되는 값이고 키를 통해 값을 변경하면 [[Value]]에 값을 재할당한다.
[[Writable]]은 변경 가능여부를 나타내며 이가 false일 경우 읽기전용프로퍼티가 된다.
[[Enumerable]]은 열거 가능 여부를 나타내며 false일 경우 for... in 문이나 Object.keys로 열거할 수 없다.
[[Configuarble]]은 재정의 가능여부를 나타내며 false일 경우 프로퍼티의 삭제, 어트리뷰트 값의 변경이 금지된다. 단, Writeable이 true일 경우 Value의 변경과 Writable을 false로 변경하는 것은 가능!

접근자 프로퍼티

접근자 프로퍼티는 자체적인 값을 갖는 프로퍼티가 아닌, 접근자함수 (get/set)으로 구성된 프로퍼티이다.

  • [[Get]] : 프로퍼티 값을 읽을 때 호출되는 접근자 함수
  • [[Set]] : 프로퍼티 값을 저장할 때 호출되는 접근자 함수
  • [[Enumerable]] : 열거 가능 여부 - Boolean (초기값 true)
  • [[Configurable]] : 재정의 가능 여부 - Boolean (초기값 true)

다음 person 객체 예시를 통해 데이터 프로퍼티와 접근자 프로퍼티를 구분해보자.

const person = {
	lastName : 'Kim',
        firstName: 'Sehan',
        
        get fullName() {
        	return '${this.firstName} ${this.lastName}
        } 
        
        set fullName(name) {
        	[this.firstName, this.lastName] = name.split('');
        }
};

console.log(person.firstName + '' + person.lastName); 
//Sehan Kim

person.fullName = 'BongSeok Lee';
console.log(person); 
//{firstName: "BongSeok", lastName: "Lee"}
//접근자 프로퍼티인 fullName은 값을 갖고 있지 않음

console.log(person.fullName); //BongSeok Kim
  • person.fullName = 'BongSeok Lee'
    접근자 프로퍼티를 통해 값을 저장하면 setter함수가 호출되어 데이터 프로퍼티 lastName과 firstName에 값을 저장한다.
  • console.log(person.fullName);
    접근자 프로퍼티에 접근하면 getter함수가 호출되어 값을 반환한다.

위 person객체 안에는 fullName앞에 get과 set가 붙은 메소드가 있는데 이것들이 getter, setter 함수이고 이들의 함수 이름 fullName이 접근자 프로퍼티이다.

= 접근자 프로퍼티는 자체적으로 Value(값)을 가지지 않으며 다만 데이터 프로퍼티의 값을 읽거나 저장할 때 관여할 뿐이다.

📌 데이터프로퍼티와 접근자프로퍼티의 프로퍼티 디스크립터 객체를 불러오면 그 둘의 어트리뷰특 다르다는 것을 확인 할 수 있다.

//데이터 프로퍼티
let descriptor = Object.getOwnPropertyDescriptor(person,'firstName');
console.log(descriptor); 
//{value: "BongSeok", writable: true, enumerable: true, configurable: true}

//접근자프로퍼티
desciptor = Object.getOwnPropertyDescriptor(person,'fullName');
console.log(descriptor); 
//{get: f, set: f, enumerable: true, configurable: true}
profile
FE 개발자 준비생 블로그 🪐

0개의 댓글