| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | 
| 26 | 27 | 28 | 29 | 30 | 31 | 
													Tags
													
											
												
												- SQL
- Brute Force
- Two Points
- 이진탐색
- Trie
- union find
- 다익스트라
- String
- binary search
- Dijkstra
- DP
- Stored Procedure
- Hash
- 그래프
- MYSQL
- two pointer
- 스토어드 프로시저
													Archives
													
											
												
												- Today
- Total
codingfarm
프로퍼티 어트리뷰트 본문
1. 내부슬롯(internal slot)과 내부 메서드(internal method)
- js engine의 구현 알고리즘을 설명하기 위해 ECMAScript 사양에서 사용하는 의사 프로퍼티(pseudo property)와 의사 메서드(pseudo method)다.
- ECMAScript 사양에 등장하는 이중 대괄호[[...]]로 감싼 이름들이 internal slot과 internal method 이다.
262.ecma-international.org/#sec-object-internal-methods-and-internal-slots
- internal slot과 internal method는 JS engine의 내부 로직이므로, javascript는 이들에 대해 직접적으로 접근하거나 호출할 방법을 제공하지 않는다.
- 단, 일부 internal slot과 internal method에 한하여 간접적으로 접근할 수 있는 수단을 제공한다.
- ex) 모든 객체는 [[Prototype]] 이라는 internal slot을 가지며, [[Prototype]] internal slot은 __proto__를 통해 간접적으로 접근할 수 있다.
 
| 1 2 3 4 | const o = {}; o.[[Prototype]]     // Uncaught SyntaxError o.__proto__         // Object.prototype | cs | 
2. 프로퍼티 어트리뷰트와 프로퍼티 디스크립터 객체
- 프로퍼티 어트리뷰트 : 프로퍼티의 상태를 나타내는 수단.
- 프로퍼티 디스크립터 객체 : 프로퍼티 어트리뷰트에 대한 정보를 제공하는 객체
- JS engine이 프로퍼티 생성시 프로퍼티 어트리뷰트를 기본값으로 자동 정의한다.
- 프로퍼티의 상태 : 프로퍼티의 값(value), 값의 갱신 가능 여부(writable), 열거 가능 여부(enumerable), 재정의 가능 여부(configurable)
- 프로퍼티 어트리뷰트 : JS engine이 관리하는 내부 상태 값(meta-property)인 내부 슬롯이다.
- ex) [[Value]], [[Writable]], [[Enumerable]], [[Configurable]] etc...
- 프로퍼티 어트리뷰트에 직접 접근할 수 없지만, Object.getOwnPropertyDescriptor 메서드를 사용하여 간접적으로 확인할 수는 있다.
- 이 함수는 프로퍼티 디스크립터(Property Descriptor) 객체를 반환한다.
- 존재하지 않는 프로퍼티나 상속받은 프로퍼티에 대한 프로퍼티 디스크립터를 요구하면 undefined가 반환된다.
 
 
 
| 1 2 3 4 5 6 | const person = {     name: 'Lee' }; console.log(Object.getOwnPropertyDescriptor(person, 'name')); // { value: 'Lee', writable: true, enumerable: true, configurable: true } | cs | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | const person = {     name: 'Lee' }; person.age = 20; console.log(Object.getOwnPropertyDescriptors(person)); /* {   name: { value: 'Lee', writable: true, enumerable: true, configurable: true },   age: { value: 20, writable: true, enumerable: true, configurable: true } } */ | cs | 
3. 데이터 프로퍼티와 접근자 프로퍼티
- 프로퍼티는 데이터 프로퍼티와 접근자 프로퍼티로 구분할 수 있다.
- 데이터 프로퍼티(data property)
- 키와 값으로 구성된 일반적인 프로퍼티
- 지금까지 살펴본 모든 프로퍼티가 이에 해당한다.
 
- 접근자 프로퍼티(accessor property)
- 자체적으로는 값을 가지 않고 다른 데이터 프로퍼티의 값을 읽거나 저장할 때 호출되는 접근자 함수(accesor function)로 구성된 프로퍼티다.
 
 
- 데이터 프로퍼티(data property)
3.1 데이터 프로퍼티
- 다음과 같은 프로퍼티 어트리뷰트를 갖는다.
- 이 프로퍼티 어트리뷰트는 JS engine이 프로퍼티를 생성할 때 기본값으로 자동 정의된다.
| 프로퍼티 어트리뷰트 | 프로퍼티 디스크립터 객체의 프로퍼티 | 설명 | 
| [[Value]] | value | - 프로퍼티 키를 통해 프로퍼티 값에 접근하면 반환되는 값 - 프로퍼티 키를 통해 프로퍼티 값을 변경하면 [[Value]]에 값을 재할당한다. 이때 프로퍼티가 없으면 프로퍼티를 동적 생성하고 생성된 프로퍼티의 [[Value]]에 값을 저장한다. | 
| [[Writable]] | writable | - 프로퍼티 값의 변경 가능 여부를 나타내며 불리언 값을 갖는다. - [[Writable]]의 값이 false인 경우 해당 프로퍼티의 [[Value]]의 값을 변경할 수 없는 읽기 전용 프로퍼티가 된다. | 
| [[Enumerable]] | enumerable | - 프로퍼티의 열거 가능 여부를 나타내며 불리언 값을 갖는다. - [[Enumerable]]의 값이 false인 경우 해당 프로퍼티는 for ... in 문이나 Object.keys 메서드 등으로 열거할 수 없다. | 
| [[Configurable]] | configurable | - 프로퍼티의 재정의 가능 여부를 나타내며 불리언 값을 갖는다. - [[Configurable]]의 값이 false인 경우 해당 프로퍼티의 삭제, 프로퍼티 어트리뷰트 값의 변경이 금지된다. - 단, [[Writable]]이 true인 경우 [[Value]]의 변경과 [[Writable]]을 false로 변경하는 것은 허용된다. | 
3.2 접근자 프로퍼티
- 자체적으로는 값을 갖지 않고 다른 데이터 프로퍼티의 값을 읽거나 저장할 때 사용(관여)하는 접근자 함수(accessor function)로 구성된 프로퍼티다.
- 접근자 프로퍼티는 다음과 같은 프로퍼티 어트리뷰트를 갖는다.
| 프로퍼티 어트리뷰트 | 프로퍼티 디스크립터 객체의 프로퍼티 | 설명 | 
| [[Get]] | get | - 접근자 프로퍼티를 통해 데이터 프로퍼티의 값을 읽을 때 호출되는 접근자 함수다. - 접근자 프로퍼티 키로 프로퍼티 값에 접근하면 프로퍼티 어트리뷰트 [[Get]]의 값, 즉 getter 함수가 호출되고 그 결과가 프로퍼티 값으로 반환된다. | 
| [[Set]] | set | - 접근자 프로퍼티를 통해 데이터 프로퍼티의 값을 저장할 때 호출되는 접근자 함수다. - 접근자 프로퍼티 키로 프로퍼티 값을 저장하면 프로퍼티 어트리뷰트 [[Set]]의 값, 즉 setter 함수가 호출되고 그 결과가 프로퍼티 값으로 저장된다. | 
| [[Enumerable]] | enumerable | 데이터 프로퍼티의 [[Enumerable]]과 같다. | 
| [[Configurable]] | configurable | 데이터 프로퍼티의 [[Configurable]]과 같다. | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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/Setter] } // 접근자 프로퍼티를 통한 프로퍼티 값의 저장 // 접근자 프로퍼티 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: [Function: get fullName],  set: [Function: set fullName],  enumerable: true,  configurable: true } | cs | 
- getter와 setter 메서드 앞에는 get과 set이 붙는다.
- getter/setter 함수의 이름 fullName이 접근자 프로퍼티다.
- 접근자 프로퍼티 fullname으로 프로퍼티 값에 접근하는 절차를 슬롯/메서드 관점에서 설명하면 아래와 같다.
- 프로퍼티 키가 유효한지 확인한다.
- 프로퍼티 키는 문자열 또는 심벌 이어야 한다.
- 프로퍼티 키 "fullName"은 문자열 이므로 유효한 프로퍼티 키다.
 
- 프로토타입 체인에서 프로퍼티를 검색한다.
- person 객체에 fullName 프로퍼티가 존재한다.
 
- 검색된 fullName 프로퍼티가 데이터 프로퍼티인지 접근자 프로퍼티인지 확인한다.
- fullName 프로퍼티는 접근자 프로퍼티다.
 
- 접근자 프로퍼티 fullName의 프로퍼티 어트리뷰트 [[Get]]의 값, 즉 getter 함수를 호출하여 그 결과를 반환한다.
- 프로퍼티 fullName의 프로퍼티 어트리 뷰트의 [[get]]의 값은 Object.getOwnPropertyDescriptor 메서드가 반환하는 프로퍼티 디스크립터 객체의 get 프로퍼티 값과 같다.
 
 
- 프로퍼티 키가 유효한지 확인한다.
프로토타입(prototype)
어떤 객체의 상위(부모) 객체의 역할을 하는 객체다.
프로토타입은 하위(자식) 객체에게 자신의프로퍼티와 메서드를 상속한다. 하위 객체는 상속받은 프로퍼티나 메서드를 자유롭게 사용할 수 있다.
프로토타입 체인
프로토 타입이 단방향 링크드 리스트 형태로 연결되어 있는 상속구조
객체의 프로퍼티나 메서드에 접근하려고 할 때 해당 객체에 접근하려는 프로퍼티 또는 메서드가 없다면 프로토타입 체인을 따라 프로토타입의 프로퍼티나 메서드를 차례대로 검색한다.
접근자 프로퍼티와 데이터 프로퍼티를 구별하는 방법
각각의 프로퍼티 디스크립터 객체의 프로퍼티가 다른것으로 파악할 수 있다.
| 1 2 3 4 5 6 7 | // 일반 객체의 __proto__는 접근자 프로퍼티다. console.log(Object.getOwnPropertyDescriptor(Object.prototype, '__proto__')); // { get: [Function: get __proto__], set: [Function: set __proto__], enumerable: false, configurable: true } // 함수 객체의 prototype은 데이터 프로퍼티다. console.log(Object.getOwnPropertyDescriptor(function() {}, 'prototype')); // { value: {}, writable: true, enumerable: false, configurable: false } | cs | 
4. 프로퍼티 정의
- 프로퍼티 정의
- 새로운 프로퍼티를 주가하면서 프로퍼티 어트리뷰트를 명시적으로 정의하는것
- 기존 프로퍼티의 프로퍼티 어트리뷰트를 재정의 하는것
- ex) 프로퍼티 값의 갱신 여부, 열거 가능 여부, 재정의 가능 여부 등을 정의한다.
 
 
- Object.defineProperty 메서드를 이용하여 프로퍼티의 어트리뷰트를 정의할 수 있다.
developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | const person = {}; // 데이터 프로퍼티 정의 Object.defineProperty(person, 'firstName', {     value: 'Ungmo',     writable : true,     enumerable: true,     configurable: true }); Object.defineProperty(person, 'lastName', {     value: 'Lee' }); let descriptor = Object.getOwnPropertyDescriptor(person, 'firstName'); console.log('firstName', descriptor); /* firstName {   value: 'Ungmo',   writable: true,   enumerable: true,   configurable: true } */ // 디스크립터 객체의 프로퍼티를 누락시키면 undefined, false가 기본값이다. descriptor = Object.getOwnPropertyDescriptor(person, 'lastName'); console.log('lastName', descriptor); /*  lastName {   value: 'Lee',   writable: false,   enumerable: false,   configurable: false } */ // [[Enumerable]]의 값이 false인 경우 // 해당 프로퍼티는 for...in 문이나 Object.keys 등으로 열거할 수 없다. // lastName 프로퍼티는 [[Enumerable]]의 값이 false 이므로 열거되지 않는다. console.log(Object.keys(person)); // [ 'firstName' ] // [[Writable]]의 값이 false인 경우 // lastName 프로퍼티는 [[Writable]]의 값이 false 이므로 값을 변경할 수 없다. // 이때 값을 변경하면 에러는 발생하지 않고 무시된다. person.lastName = 'Kim'; // [[Configurable]]의 값이 false 인경우 해당 프로퍼티를 삭제할 수 없다. // lastName 프로퍼티는 [[Configurable]]의 값이 false 이므로 삭제할 수 없다. // 이때 프로퍼티를 삭제하면 에러는 발생하지 않고 무시된다. delete person.lastName; // [[Configurable]]의 값이 false인 경우 해당 프로퍼티를 재정의 할 수 없다. // Object.defineProperty(person, 'lastName', { enumerable: true }); // Uncaught TypeError: Cannot redefine property: lastName descriptor = Object.getOwnPropertyDescriptor(person, 'lastName'); console.log('lastName', descriptor); /* lastName {   value: 'Lee',   writable: false,   enumerable: false,   configurable: false } */ // 접근자 프로퍼티 정의 Object.defineProperty(person, 'fullName', {     get() {         return `${this.firstName} ${this,lastName}`     },     set(name) {         [this.firstName, this.lastName] = name.split(' ');     },     enumerable: true,     configurable: true }); descriptor = Object.getOwnPropertyDescriptor(person, 'fullName'); console.log('fullName', descriptor); /* fullName {   get: [Function: get],   set: [Function: set],   enumerable: true,   configurable: true } */ person.fullName = 'Heegun Lee'; console.log(person); // { firstName: 'Heegun', fullName: [Getter/Setter] } | cs | 
Object.defineProperty 메서드로 프로퍼티를 정의할 때 프로퍼티 디스크립터 객체의 프로퍼티를 일부 생략할 수 있다.
새략된 어트리뷰트는 다음과 같이 기본값이 적용된다.
| 프로퍼티 디스크립터 객체의 프로퍼티 | 대응하는 프로퍼티 어트리뷰트 | default value | 
| value | [[Value] | undefined | 
| get | [[Get]] | undefined | 
| set | [[Set]] | undefined | 
| writable | [[Writable]] | false | 
| enumerable | [[Enumerable]] | false | 
| configurable | [[Configurable]] | false | 
Object.defineProperty 메서드는 한번에 하나의 프로퍼티만 정의할 수 있다.
Object.defineProperties 메서드로 여러개의 프로퍼티를 한번에 정의할 수 있다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | const person = {}; Object.defineProperties(person, {     // 데이터 프로퍼티 정의     firstName : {         value: 'Ungmo',         writable: true,         enumerable: true,         configurable: true     },     lastName: {         value: 'Lee',         writable: true,         enumerable: true,         configrable: true     },     // 접근자 프로퍼티 정의     fullName: {         get() {             return `${this.firstName} ${this.lastName}`;         },         set(name) {             [this.firstName, this.lastName] = name.split(' ');         },         enumerable: true,         configurable: true     } }); person.fullName = 'Heegun Kim'; console.log(person); //  { firstName: 'Heegun', lastName: 'Lee', fullName: [Getter/Setter] } | cs | 
5. 객체 변경 방지
자바스크립트는 객체의 변경을 방지하는 다양한 메서드를 제공한다.
객체 변경 방지 메서드들은 객체의 변경을 금지하는 강도가 다르다.
| 구분 | 메서드 | 프로퍼티 추가 | 프로퍼티 삭제 | 프로퍼티 값 읽기 | 프로퍼티 값 쓰기 | 프로퍼티 어트리뷰트 재정의 | 
| 객체 확장 금지 | object.preventExtensions | X | O | O | O | O | 
| 객체 밀봉 | Object.seal | X | X | O | O | X | 
| 객체 동결 | Object.freeze | X | X | O | X | X | 
5.1 객체 확장 금지(prevent extension)
- 정의 : 프로퍼티 추가 금지
- 동적추가와 Object.defineProperty 메서드로 추가 2가지 방법이 모두 금지된다.
 
- Object.preventExtensions 메서드로 객체의 확장을 금지한다.
- Object.isExtensible 메서드로 객체의 확장 가능 여부를 확인할 수 있다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | const person = { name: 'Lee' }; // person 객체는 확장이 금지된 객체가 아니다. console.log(Object.isExtensible(person));   // true // person 객체의 확장을 금지하여 프로퍼티 추가를 금지한다. Object.preventExtensions(person); // person 객체는 확장이 금지된 객체다. console.log(Object.isExtensible(person));   // false // 프로퍼티 추가가 금지된다. person.age = 20;    // ignore, strict mode 에서는 에러 console.log(person);  // { name: 'Lee' } // 프로퍼티 추가는 금지되지만 삭제는 가능하다. delete person.name; console.log(person); // {} Object.defineProperty(person, 'age', {value: 20}); // TypeError | cs | 
5.2 객체 밀봉(seal)
- 정의 : 프로퍼티 추가 및 삭제와 프로퍼티 어트리뷰트 재정의 금지
- 밀봉된 객체는 읽기와 쓰기만 가능하다.
- Object.seal 메서드로 밀봉할 수 있다.
- Object.isSealed 메서드로 밀봉 여부를 확인할 수 있다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | const person = { name: 'Lee' }; // person 객체는 밀봉(seal)된 객체가 아니다. console.log(Object.isSealed(person));   // false // person 객체를 밀봉(seal)하여 프로퍼티 추가, 삭제, 재정의를 금지한다. Object.seal(person); // person 객체는 밀봉(seal)된 객체다. console.log(Object.isSealed(person));   // true // 밀봉(seal)된 객체는 configurable이 false다. console.log(Object.getOwnPropertyDescriptors(person)); /* {   name: {     value: 'Lee',     writable: true,     enumerable: true,     configurable: false   } } */ // 프로퍼티의 추가 및 삭제가 금지된다. // 아래 코드는 무시되며, strict mode 에서는 에러 발생 person.age = 20; delete person.name; person.name = "Kim"; console.log(person); // { name: 'Kim' } Object.defineProperty(person, 'name', { configurable: true }); // TypeError | cs | 
5.3 객체 동결(freeze)
- 정의 : 프로퍼티 추가 및 삭제와 어트리뷰트 재정의 금지, 프로퍼티 값 갱신 금지
- 즉, 동결된 객체는 읽기만 가능하다.
 
- Object.freeze 메서드로 객체를 동결 시킬 수 있다.
- Object.isFrozen 메서드로 동결 여부를 확인할 수 있다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | const person = { name: 'Lee' }; // person 객체는 동결된 객체가 아니다. console.log(Object.isFrozen(person));   // false // person 객체를 동결한다. Object.freeze(person); // person 객체는 동결된 객체다. console.log(Object.isFrozen(person));   // true // 동결(freeze)된 객체는 writable과 configurable이 false이다. console.log(Object.getOwnPropertyDescriptors(person)); /* {   name: {     value: 'Lee',     writable: false,     enumerable: true,     configurable: false   } } */ // 프로퍼티 추가, 삭제, 값 갱신이 금지된다. person.age = 20; delete person.name; person.name = 'Kim' console.log(person); // { name: 'Lee' } Object.defineProperty(person, 'name', {configurable: true}); // TypeError | cs | 
5.4 불변 객체
- 앞선 변경 방지 메서드들은 얕은 변경 방지(shallow only)로 직속 프로퍼티만 변경이 방지되고 중첩 객체까지는 영향을 주지는 못한다.
- 가령 Object.freeze 메서드로 객체를 동결하여도 중첩 객체 까지 동결 수없다.
 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | const person = {     name: 'Lee',     address: { city: 'Seoul' } }; // 얕은 객체 동결 Object.freeze(person); // 직속 프로퍼티만 동결한다. console.log(Object.isFrozen(person));           // true // 중첩 객체까지 동결하지 못한다. console.log(Object.isFrozen(person.address));   // false person.address.city = 'Busan'; console.log(person); // { name: 'Lee', address: { city: 'Busan' } } | cs | 
- 모든 프로퍼티에 대해 재귀적으로 Object.freeze 메서드를 호출하여 중첩 객체에 대해서 까지 동결 할 수 있다.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | function deepFreeze(target) {     // 객체가 아니거나 동결된 객체는 무시하고, 객체이면서 동결되지 않은 객체만 동결한다.     if (target && typeof target === 'object' && !Object.isFrozen(target)) {         Object.freeze(target);         /*             모든 프로퍼티를 순회하며 재귀적으로 동결한다.             Object.keys 메서드 : 객체 자신의 열거 가능한 프로퍼티 키를 배열로 반환한다.             forEach 메서드 : 배열을 순회하며 배열의 각 요소에 대하여 콜백 함수를 실행한다.         */     }     return target; } const person = {     name: 'Lee',     address: { city: 'Seoul' } }; deepFreeze(person); console.log(Object.isFrozen(person));           // true // 중첩 객체도 동결된다. console.log(Object.isFrozen(person.address));   // true person.address.city = 'Busan'; console.log(person);    // { name: 'Lee', address: { city: 'Busan' } } | cs | 
'Programming Language > JavaScript' 카테고리의 다른 글
| 전역 변수의 문제점 (0) | 2021.03.08 | 
|---|---|
| 변수의 생명주기 (0) | 2021.03.08 | 
| 스코프(scope) (0) | 2021.03.07 | 
| var와 let, const (0) | 2021.03.07 | 
| 함수 (0) | 2021.03.04 | 
			  Comments
			
		
	
               
           
					
					
					
					
					
					
				