-
Notifications
You must be signed in to change notification settings - Fork 0
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
16장 #8
Conversation
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.
고생하셨습니다! 내용이 상당히 어렵네요
그렇다면 프로퍼티 어트리뷰트는 무엇인가? | ||
|
||
> [!note] **프로퍼티 어트리뷰트**란? | ||
>js 엔진이 관리하는 프로퍼티가 가지는 내부 상태값으로 | ||
>해당 프로퍼티의 내부 슬롯 `[[Value]]`,`[[Writeable]]`, `[[Enumerable]]`, `[[Configurable]]`이다. | ||
|
||
- `[[Value]]` : 프로퍼티의 값 | ||
- `[[Writeable]]` : 프로퍼티의 값의 갱신 가능 여부 | ||
- `[[Enumerable]]` : 프로퍼티의 열거 가능 여부 | ||
- `[[Configurable]]` : 프로퍼티의 재정의 가능 여부 | ||
|
||
따라서, 프로퍼티 어트리뷰트는 내부 슬롯이기에 직접 값에 접근할 수는 없지만 간접적으로는 확인이 가능하다. | ||
`Object.getOwnPropertyDescriptor()` 메소드를 활용하여 간접적으로 확인이 가능하다. |
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.
일종의 class에서 private 필드 속성들을 정의해 놓고, getter로 접근하는 방식과 비슷하네요!
get fullName(){ //getter | ||
return `${this.firstName} ${this.lastName}`; | ||
}, | ||
|
||
set fullName(name){ //setter | ||
[this.firstName, this.lastName] = name.split(' '); |
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.
JS도 getter, setter를 따로 제공해주고 있었군요?
그런데 굳이 get, set 접근자 프로퍼티를 이용해서 선언하는 것과, 일반적인 getfullName()과 setfullName으로 선언하는 것과의 차이가 궁금합니다.
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.
굉장히 자바스러운 코드를 짤 수 있게 도와줄 것 같아요...
@Turtle-Hwan 동적으로 객체의 프로퍼티를 호출할 때, 예를 들어
const prop = "fullName";
console.log(persion[prop]);
과 같이 사용할 때는 유의미한 차이가 있겠네요
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.
방금 해봤더니 "프로퍼티" 라고 부르는 이유가 있었네요!
const person = {
//데이터 프로퍼티
firstName:'Ungmo',
lastName: 'Lee',
//접근자 프로퍼티 -> 접근자 함수 fullName으로 정의
get fullName(){ //getter
return `${this.firstName} ${this.lastName}`;
},
set fullName(name){ //setter
[this.firstName, this.lastName] = name.split(' ');
//배열 디스트럭처링 할당 -> 배열의 각 요소를 변수에 할당(31장에서 자세히 살펴보자)
}
};
const person2 = {
firstName: 'ungmo',
lastName: 'lee',
getFullName() {
return `${this.firstName} ${this.lastName}`;
},
setFullName(name) {
[this.firstName, this.lastName] = name.split(' ');
}
}
이렇게 했을 때,
person의 get, set은 접근자 "프로퍼티" 라서
person.fullName
으로 접근해야 합니다.
그런데 person2의 경우, person2.getFullName()
으로 괄호를 붙여서 "함수" 를 실행시켜야 원하는 결과를 얻을 수 있습니다
아주 신기하네요!! 왜 프로퍼티로 getter, setter를 만들어 두었을까요??
| 프로퍼티 디스크립터 객체의 프로퍼티 | 프로퍼티 어트리뷰트 | 생략 시 기본값 | | ||
| ------------------- | ------------------ | --------- | | ||
| value | `[[Value]]` | undefined | | ||
| get | `[[Get]]` | undefined | | ||
| set | `[[Set]]` | undefined | | ||
| writable | `[[Writable]]` | false | | ||
| enumerable | `[[Enumerable]]` | false | | ||
| configurable | `[[Configurable]]` | false | |
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.
const person = {
firstName: 'kim'
}
이렇게만 선언하고 get, set을 지정하지 않으면 undefined가 되고, person.firstName으로 바로 접근 가능합니다.
그런데 es19 이전엔 '#' 처럼 class의 private field가 없었는데 굳이 get, set을 지정해야 할 당위성도 부족한 것 같고,
js의 장점이 객체의 다양한 선언법과 getter, setter 없이 JSON 내의 프로퍼티에 자유롭게 접근하고 값을 수정할 수 있는 것이라 생각하는데,
굳이 get, set property attribute가 존재하는 이유를 정말 모르겠어요!
여러분의 생각이 궁금합니다!!!
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.
JS는 객체지향 프로그래밍인가요? 함수형 프로그래밍인가요?
지환님이 말씀해주신 것처럼 JS는 Class를 사용하는 대신 객체로 이를 대신하도록 만들어졌습니다.
그러나 당시 Class없이 프로그래밍 하는 것이 말도 안된다고 생각한 흐름 탓에 JS도 Class에 관한 문법들을 지원하게 된 것 같습니다!
위 글에서 JS가 객체지향인지 함수형인지에 대한 설명을 깔끔하게 정리해 놓았다고 생각합니다. 다들 시간 나실 때 한번씩 읽어보시는 것도 좋을 것 같아요! 😸
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.freeze()
같은 메소드는 함수형 프로그래밍을 지원하기 위해 필요성이 있을 수도 있다고 생각하는데 get
, set
등과 같은 프로퍼티 속성은 명시적 접근 제어자가 없는 JS에서 필요한지 의문스럽습니다.
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.
테오님의 글이네요 너무 좋은 글입니다!!
저도 마찬가지로 객체를 클래스처럼 사용하기 위해 지원하는 기능이라고 생각해요. 많은 사람들이 익숙해하는 방식이니 지원하지 않을 수 없었던 것 아닐까요? ES6에 클래스가 도입된 것 처럼요,,
여담이지만 자바스크립트 개발에 참여하시고 JSON을 만드신 더글라스 크록포드는 ES6 출시 당시 새로운 기능들을 소개하며 Class 문법이 ES6의 가장 나쁜점이라고 하셨습니다. 자바스크립트의 철학이 훼손되는게 싫으셨나봐요,, 자바 개발자들이 공부하기 싫어서 계속 Class 기능을 요구한다며 자신들이 얼마나 비참한지 모르고 죽을거라고 신랄하게 까시더군요 😇
영상 - 우리가 지금까지 공부한 스프레드 연산자, 모듈, let, 구조분해 할당 등 ES6의 내용도 소개하고 계시니 한 번 살펴보세용 10년전 영상이지만 재밌습니다
| 객체 확장 금지 | `Object.preventExtensions` | X | O | O | O | O | | ||
| 객체 밀봉 | `Object.seal` | X | X | O | O | X | | ||
| 객체 동결 | `Object.freeze` | X | X | O | X | X | |
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.freeze` 메소드 호출 |
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.
-
연산량이 상당할 것 같은데 처음에만 작동하니 괜찮은 걸지 궁금하네요
-
strict mode가 아니면 에러를 띄우지 않고 무시해서 오히려 대형 프로젝트에선 유지보수가 어려워질 수 있다는 생각이 드는데, 차라리 typescript로 object들 type을 열심히 관리하는게 좋지 않을까라는 의문도 드네요?
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.
아무래도 타입스크립트가 . . . 좋은 것 같아요 . . . 😢
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.
지환님이랑 성종님이 말씀해주신 대로 TS의 readonly
를 사용하는 게 더 좋을 수도 있을 것 같아요. 그런데 찾아보니 TS에서 중첩 객체에 readonly
를 이용하려면 결국 타입을 재귀적으로 선언해야 하네요.
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.
type DeepReadonly<T> = {
readonly [K in keyof T]: T[K] extends object
? DeepReadonly<T[K]>
: T[K];
};
TS에서도 이런식으로 사용하쥬,,
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.
프로퍼티에 대해서 깊게 생각해볼 수 있었던 것 같아요! 정리하시느라 고생하셨습니다~~ 👍 👍 👍
//2. 접근자 프로퍼티를 통한 프로퍼티 값의 저장 | ||
person.fullName = "Heegun Lee"; //setter 접근자 함수 호출 | ||
console.log(person); //{firstName: "Heegun", lastName: "Lee"} | ||
|
||
//3. 접근자 프로퍼티를 통한 프로퍼티 값의 참조 | ||
console.log(person.fullName); //getter 함수 호출 | ||
//Heegun Lee |
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.
접근자 프로퍼티는 실제로 fullName이라는 프로퍼티를 가지고 있지 않지만
getter와 setter 함수를 통해 fullName이라는 가상의 프로퍼티에 접근할 수 있게 해주는군요!
>[!tip] 프로토타입이란? | ||
> 프로토타입은 어떤 객체의 상위(부모) 객체의 역할을 하는 객체다. | ||
> 프로토타입은 하위 객체에게 자신의 프로퍼티와 메서드를 상속한다 | ||
> 자세한 내용은 19장에서..살펴보도록 하자 |
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.
JS는 어쨰서? 왜? 프로토타입을 선택했나?
동민님이 처음 스터디 할 때 알려주신 글이 떠오르네요 👀
//3. [[Enumerable]]의 값이 false인 경우 -> Object.keys로 열거되지 않는다. | ||
console.log(Object.keys(person)); | ||
// ["firstName"] |
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.
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.values
키와 값을 모두 뽑아주는 Object.entries
그리고 for ... in
등 enumerable
속성을 사용하는 것들이 많더라구요!
| 프로퍼티 디스크립터 객체의 프로퍼티 | 프로퍼티 어트리뷰트 | 생략 시 기본값 | | ||
| ------------------- | ------------------ | --------- | | ||
| value | `[[Value]]` | undefined | | ||
| get | `[[Get]]` | undefined | | ||
| set | `[[Set]]` | undefined | | ||
| writable | `[[Writable]]` | false | | ||
| enumerable | `[[Enumerable]]` | false | | ||
| configurable | `[[Configurable]]` | false | |
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.
JS는 객체지향 프로그래밍인가요? 함수형 프로그래밍인가요?
지환님이 말씀해주신 것처럼 JS는 Class를 사용하는 대신 객체로 이를 대신하도록 만들어졌습니다.
그러나 당시 Class없이 프로그래밍 하는 것이 말도 안된다고 생각한 흐름 탓에 JS도 Class에 관한 문법들을 지원하게 된 것 같습니다!
위 글에서 JS가 객체지향인지 함수형인지에 대한 설명을 깔끔하게 정리해 놓았다고 생각합니다. 다들 시간 나실 때 한번씩 읽어보시는 것도 좋을 것 같아요! 😸
| -------- | -------------------------- | --- | --- | ---- | ---- | -------------- | | ||
| 객체 확장 금지 | `Object.preventExtensions` | X | O | O | O | O | | ||
| 객체 밀봉 | `Object.seal` | X | X | O | O | X | | ||
| 객체 동결 | `Object.freeze` | X | X | O | X | X | |
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.freeze` 메소드 호출 |
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.
아무래도 타입스크립트가 . . . 좋은 것 같아요 . . . 😢
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.
늘 공부할 때마다 "음 이런 게 있구나"하고 신기해 하면서도 언제 쓸지 잘 모르겠는 챕터입니다 🤔. 계속 개발을 하다보면 언젠가 쓸 날이 올까요? 정리하느라 수고하셨어요!
get fullName(){ //getter | ||
return `${this.firstName} ${this.lastName}`; | ||
}, | ||
|
||
set fullName(name){ //setter | ||
[this.firstName, this.lastName] = name.split(' '); |
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.
굉장히 자바스러운 코드를 짤 수 있게 도와줄 것 같아요...
@Turtle-Hwan 동적으로 객체의 프로퍼티를 호출할 때, 예를 들어
const prop = "fullName";
console.log(persion[prop]);
과 같이 사용할 때는 유의미한 차이가 있겠네요
|
||
//데이터 프로퍼티 정의 | ||
Object.defineProperty(person, 'firstName', { | ||
value: 'Ungmo', |
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.
이 사람의 이름은 '웅'모일까요 '응'모일까요?
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.
웅! 일것 같아요
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.
이책 저자분 성함이 이웅모 님이시더군요 하하
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.
이름 귀엽다
//3. [[Enumerable]]의 값이 false인 경우 -> Object.keys로 열거되지 않는다. | ||
console.log(Object.keys(person)); | ||
// ["firstName"] |
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.values
키와 값을 모두 뽑아주는 Object.entries
그리고 for ... in
등 enumerable
속성을 사용하는 것들이 많더라구요!
| 프로퍼티 디스크립터 객체의 프로퍼티 | 프로퍼티 어트리뷰트 | 생략 시 기본값 | | ||
| ------------------- | ------------------ | --------- | | ||
| value | `[[Value]]` | undefined | | ||
| get | `[[Get]]` | undefined | | ||
| set | `[[Set]]` | undefined | | ||
| writable | `[[Writable]]` | false | | ||
| enumerable | `[[Enumerable]]` | false | | ||
| configurable | `[[Configurable]]` | false | |
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.freeze()
같은 메소드는 함수형 프로그래밍을 지원하기 위해 필요성이 있을 수도 있다고 생각하는데 get
, set
등과 같은 프로퍼티 속성은 명시적 접근 제어자가 없는 JS에서 필요한지 의문스럽습니다.
*/ | ||
|
||
//2. 프로퍼티 추가 금지 | ||
person.age = 20; // 그냥 무시됨. strict mode에서는 에러 발생 |
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.
strict mode를 안 사용하면 개발자 입장에서는 나는 값을 수정했다고 생각할 수도 있으니 반드시 사용하는 게 좋을 것 같아요
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.
strict mode를 기본적으로 적용할 수 있는지 찾다가 이상한 js 예약어를 발견했습니다. ES module로 선언된 파일들은 전부 기본적으로 strict mode가 적용된다고 하네요!
불변 객체를 구현 방법 | ||
=> 객체를 값으로 가지는 모든 프로퍼티에 대해 `Object.freeze` 메소드 호출 |
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.
지환님이랑 성종님이 말씀해주신 대로 TS의 readonly
를 사용하는 게 더 좋을 수도 있을 것 같아요. 그런데 찾아보니 TS에서 중첩 객체에 readonly
를 이용하려면 결국 타입을 재귀적으로 선언해야 하네요.
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.defineProperty(person, 'firstName', { | ||
value: 'Ungmo', |
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.freeze` 메소드 호출 |
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.
type DeepReadonly<T> = {
readonly [K in keyof T]: T[K] extends object
? DeepReadonly<T[K]>
: T[K];
};
TS에서도 이런식으로 사용하쥬,,
| 프로퍼티 디스크립터 객체의 프로퍼티 | 프로퍼티 어트리뷰트 | 생략 시 기본값 | | ||
| ------------------- | ------------------ | --------- | | ||
| value | `[[Value]]` | undefined | | ||
| get | `[[Get]]` | undefined | | ||
| set | `[[Set]]` | undefined | | ||
| writable | `[[Writable]]` | false | | ||
| enumerable | `[[Enumerable]]` | false | | ||
| configurable | `[[Configurable]]` | false | |
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.
테오님의 글이네요 너무 좋은 글입니다!!
저도 마찬가지로 객체를 클래스처럼 사용하기 위해 지원하는 기능이라고 생각해요. 많은 사람들이 익숙해하는 방식이니 지원하지 않을 수 없었던 것 아닐까요? ES6에 클래스가 도입된 것 처럼요,,
여담이지만 자바스크립트 개발에 참여하시고 JSON을 만드신 더글라스 크록포드는 ES6 출시 당시 새로운 기능들을 소개하며 Class 문법이 ES6의 가장 나쁜점이라고 하셨습니다. 자바스크립트의 철학이 훼손되는게 싫으셨나봐요,, 자바 개발자들이 공부하기 싫어서 계속 Class 기능을 요구한다며 자신들이 얼마나 비참한지 모르고 죽을거라고 신랄하게 까시더군요 😇
영상 - 우리가 지금까지 공부한 스프레드 연산자, 모듈, let, 구조분해 할당 등 ES6의 내용도 소개하고 계시니 한 번 살펴보세용 10년전 영상이지만 재밌습니다
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.
깊고 어려운 내용이네요
정리하느라 고생하셨어요!
## 16.5 객체 변경 방지 | ||
객체는 변경 가능한 값이므로 재할당 없이 직접 변경할 수 있다. | ||
-> 즉, 프로퍼티를 추가 / 삭제 / 갱신할 수 있으며 프로퍼티 어트리뷰트를 재정의할 수도 있다. | ||
-> js는 객체의 변경을 방지의 강도가 다양한 메서드를 제공한다. | ||
|
||
| 구분 | 메서드 | 추가 | 삭제 | 값 읽기 | 값 쓰기 | 프로퍼티 어트리뷰트 재정의 | | ||
| -------- | -------------------------- | --- | --- | ---- | ---- | -------------- | | ||
| 객체 확장 금지 | `Object.preventExtensions` | X | O | O | O | O | | ||
| 객체 밀봉 | `Object.seal` | X | X | O | O | X | | ||
| 객체 동결 | `Object.freeze` | X | X | O | X | X | |
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.
const와 object.freeze를 활용해 객체를 동결하는 예시가 있어서 가져와봤습니다
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.freeze를 활용해 불변성을 유지하는 경우가 흔한가요? 불변 객체를 만들어 사용하는 대신 immer.js 같은 라이브러리를 사용하는 경우도 있는데 장단점을 비교해보면 좋을것 같아요
(immer.js는 객체를 직접 수정하는 것처럼 보이지만, 실제로는 새로운 불변 객체를 반환한다고 하네요)
get fullName(){ //getter | ||
return `${this.firstName} ${this.lastName}`; | ||
}, | ||
|
||
set fullName(name){ //setter | ||
[this.firstName, this.lastName] = name.split(' '); |
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.
방금 해봤더니 "프로퍼티" 라고 부르는 이유가 있었네요!
const person = {
//데이터 프로퍼티
firstName:'Ungmo',
lastName: 'Lee',
//접근자 프로퍼티 -> 접근자 함수 fullName으로 정의
get fullName(){ //getter
return `${this.firstName} ${this.lastName}`;
},
set fullName(name){ //setter
[this.firstName, this.lastName] = name.split(' ');
//배열 디스트럭처링 할당 -> 배열의 각 요소를 변수에 할당(31장에서 자세히 살펴보자)
}
};
const person2 = {
firstName: 'ungmo',
lastName: 'lee',
getFullName() {
return `${this.firstName} ${this.lastName}`;
},
setFullName(name) {
[this.firstName, this.lastName] = name.split(' ');
}
}
이렇게 했을 때,
person의 get, set은 접근자 "프로퍼티" 라서
person.fullName
으로 접근해야 합니다.
그런데 person2의 경우, person2.getFullName()
으로 괄호를 붙여서 "함수" 를 실행시켜야 원하는 결과를 얻을 수 있습니다
아주 신기하네요!! 왜 프로퍼티로 getter, setter를 만들어 두었을까요??
#### 접근자 프로퍼티와 데이터 프로퍼티 구별하기 | ||
```js | ||
//일반 객체의 __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} | ||
``` |
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.
//일반 객체의 __proto__ => 접근자 프로퍼티
Object.getOwnPropertyDescriptor(Object.prototype, '__proto__');
// {get: f, set:f, enumerable: false, configurable: true}
//함수 객체의 prototype => 데이터 프로퍼티
Object.getOwnPropertyDescriptor(Object, 'prototype');
// {value: { ... }, writable: true, enumerable: false, configurable: false}
value: 'Ungmo', | ||
writable: true, | ||
enumerable: true, | ||
configurable: true |
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.
한 번 configurable을 false로 했을 때 configurable을 다시 true로 바꿀 수 없다!!
writable 도 마찬가지!
|
||
//데이터 프로퍼티 정의 | ||
Object.defineProperty(person, 'firstName', { | ||
value: 'Ungmo', |
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.
이름 귀엽다
```js | ||
//일반 객체의 __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} | ||
``` |
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.
@whddltjdwhd 의 질문
Q. Object.getOwnPropertyDescriptor(Object.prototype, 'prototype');
Object.getOwnPropertyDescriptor(function() {}, '__proto__');
이렇게 바꿔서 입력하면 어떻게 되나요?
A. 둘 다undifined
가 나온다.
//모든 프로퍼티 순회하면서 재귀적으로 동결 | ||
Object.keys(target).forEach(key => deepFreeze(target[key])); | ||
//keys : 객체 자신의 열거 가능한 프로퍼티 키를 배열로 반환 | ||
//forEach : 배열을 순회하며 각 요소에 대해 콜백 함수 실행 |
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.
으악 번거롭네요
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.
수고하셨습니다아 ㅠ0ㅠ
16장 프로퍼티 어트리뷰트입니다~ 🍥