-
Notifications
You must be signed in to change notification settings - Fork 2
[sep-3rd]유진-학습정리 #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yujin-fe
wants to merge
1
commit into
sep-3rd
Choose a base branch
from
yujin
base: sep-3rd
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[sep-3rd]유진-학습정리 #18
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| # 9월 3주차(16장) | ||
|
|
||
| 날짜: 2025년 9월 18일 | ||
|
|
||
| # 16.1/16.2 프로퍼티 어트리뷰트와 디스크립터 객체 | ||
|
|
||
| ## 내부 슬롯과 내부 메소드 | ||
|
|
||
| 자바스크립트 엔진이 구현 알고리즘을 설명하기 위해 사용하는 의사 프로퍼티와 의사메소드 | ||
|
|
||
| - 의사프로퍼티/의사 메소드: 실제로 있는 프로퍼티나 메소드가 아니지만 그렇게 동작하는 것 | ||
| - 개발자가 직접적으로 접근, 호출 불가 | ||
| - 일부 내부 슬롯과 내부 메소드에는 간접적으로 접근할 수 있는 수단 존재. | ||
|
|
||
| ## 프로퍼티 어트리뷰트와 프로퍼티 디스크립터 객체 | ||
|
|
||
| - 프로퍼티 어트리뷰트: 프로퍼티의 상태를 나타냄 | ||
| - [[Value]] -프로퍼티 값 | ||
| - [[Writable]]-값의 변경 가능 여부 | ||
|
|
||
| `Object.defineProperty` | ||
|
|
||
| 1. 기존 객체에 새 프로퍼티를 정의할 때 | ||
| 2. 기존 프로퍼티의 디스크립터를 수정할 때 | ||
|
|
||
| ```jsx | ||
| const myFamily ={ | ||
| human:"유진" | ||
| malecat:"후추" | ||
| femalecat:"체리" | ||
| } | ||
| ``` | ||
|
|
||
| 1) malecat이 `[[Writable]]: true` (값 변경 가능) | ||
|
|
||
| ```jsx | ||
| Object.defineProperty(myFamily, 'malecat', { | ||
| value: "후추", | ||
| writable: true | ||
| }); | ||
|
|
||
| obj.malecat = "소금"; // 값 변경 가능 | ||
| console.log(obj.malecat); // "소금" | ||
| ``` | ||
|
|
||
| 2) malecat이 `[[Writable]]: false` (값 변경 불가) | ||
|
|
||
| ```jsx | ||
| Object.defineProperty(myFamily, 'malecat', { | ||
| value: "후추", | ||
| writable: false | ||
| }); | ||
|
|
||
| obj.malecat = "소금"; // 값 변경 시도 무시 | ||
| console.log(obj.malecat); // "후추" | ||
| ``` | ||
|
|
||
| - [[Enumerable]]-열거 가능 여부 | ||
|
|
||
| ```jsx | ||
| const myFamily = { | ||
| human: "유진", | ||
| malecat: "후추", | ||
| femalecat: "체리" | ||
| }; | ||
| ``` | ||
|
|
||
| 1-1) malecat이 `Enumerable: true`일 때 | ||
|
|
||
| ```jsx | ||
| for (let key in myFamily) { | ||
| console.log(key); | ||
| } | ||
| //human | ||
| //malecat | ||
| //femalecat | ||
| ``` | ||
|
|
||
| - 반복문으로 모두 접근 가능 | ||
|
|
||
| ```jsx | ||
| console.log(Object.keys(myFamily)); //["human", "malecat", "femalecat"] | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 1-2) malecat이 `Enumerable: false`일 때 | ||
|
|
||
| ```jsx | ||
| Object.defineProperty(myFamily, 'malecat', { | ||
| value: "후추", | ||
| enumerable: false | ||
| }); | ||
|
|
||
| for (let key in myFamily) { | ||
| console.log(key); | ||
| } | ||
| //human | ||
| //femalecat | ||
|
|
||
| ``` | ||
|
|
||
| - malecat은 반복문에서 제외됨 | ||
|
|
||
| ```jsx | ||
| console.log(Object.keys(myFamily));//["human", "femalecat"] | ||
| ``` | ||
|
|
||
| 요약: [[Enumerable]] = true → 반복문/열거 가능, false → 반복문에서 숨김 | ||
|
|
||
| - [[Configurable]]-프로퍼티 어트리뷰트 값의 변경 가능 여부 | ||
| - 프로퍼티 어트리뷰트 자체(writable, enumerable, configurable)와 삭제 가능 여부 결정 | ||
| - true → 어트리뷰트 변경 가능, 속성 삭제 가능 | ||
| - false → 어트리뷰트 변경 불가, 속성 삭제 불가 (값의 변경은 writable이면 가능) | ||
|
|
||
| ```jsx | ||
| const obj = {}; | ||
| Object.defineProperty(obj, 'x', { value: 10, writable: true, configurable: false }); | ||
|
|
||
| // 속성 변경 시도 | ||
| Object.defineProperty(obj, 'x', { emunerable: false }); // ❌ TypeError | ||
| Object.defineProperty(obj, 'x', { writable: false });//에러 안남 | ||
| delete obj.x; // ❌ 삭제 불가 | ||
| obj.x = 20; // ✅ 값 자체는 writable이면 변경 가능 | ||
| ``` | ||
|
|
||
| > 한 번 configurable을 false로 지정하면 다시 true로 바꿀 수 없음 | ||
| > | ||
|
|
||
| → why? configurable이 false이기 때문에 프로퍼티 어트리뷰트인 configurable의 값을 변경하지 못한다. | ||
|
|
||
| ⚠️ 주의: `writable`은 예외적으로 `true → false`로 바꾸는 건 가능. | ||
|
|
||
| - Q: 프로퍼티 어트리뷰트는 공유되는가? | ||
| - **각 프로퍼티마다 각각 가지고 있음** | ||
| - human, malecat, femalecat 각각 독립적으로 [[Writable]], [[Enumerable]], [[Configurable]] 상태를 가짐. | ||
| - 따라서 하나를 바꾼다고 다른 프로퍼티에는 영향 없음 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ```jsx | ||
| Object.defineProperty(obj, 'malecat', { enumerable: false }); | ||
| console.log(Object.keys(obj)); // ["human", "femalecat"] | ||
| console.log(Object.getOwnPropertyDescriptor(obj, 'human').enumerable); | ||
| // true | ||
| //malecat만 false로 바꿨지만 human은 그대로 true유지 | ||
| ``` | ||
|
|
||
| - 디스크립터 객체: 프로퍼티 어트리뷰트를 나타내는 객체 | ||
|
|
||
| ```jsx | ||
| const myFamily = { | ||
| human: "유진", | ||
| malecat: "후추", | ||
| femalecat: "체리" | ||
| }; | ||
|
|
||
| // 예시: 디스크립터 객체 가져오기 | ||
| console.log(Object.getOwnPropertyDescriptor(myFamily, "human")); | ||
|
|
||
| { | ||
| value: "유진", // 프로퍼티 값 | ||
| writable: true, // 값 변경 가능 | ||
| enumerable: true, // 열거 가능 | ||
| configurable: true // 삭제하거나 어트리뷰트 변경 가능 | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.__proto__가 그중 하나죠~~!