Open
Conversation
hannut91
reviewed
Sep 4, 2023
Comment on lines
+75
to
+81
| keys() { | ||
| return this.#keys; | ||
| } | ||
|
|
||
| values() { | ||
| return this.#values; | ||
| } |
Contributor
There was a problem hiding this comment.
객체의 private속성인 키 목록과 값 목록을 접근할 수 있도록 메서드를 만들어 주셨네요.
객체 안에 있는 참조 타입을 외부로 그대로 반환하게 되면 캡슐화 원칙을 깨트릴 수 있어요. 예를들어서
class Something {
#items = [];
getItems() {
return this.#items;
}
}
const something = new Something();
const innerItems = something.getItems();
innerItems[0] = 'Hello';
console.log(something.getItems()); // ['Hello']이런식으로 외부에서 값을 수정할 수 있게 되는거죠. 그래서 바깥으로 무언가를 전달할 때는 항상 방어적으로 작성해야 해요.
class Something {
#items = [];
getItems() {
return [...this.#items]; // 복사해서 반환
}
}
const something = new Something();
const innerItems = something.getItems();
innerItems[0] = 'Hello';
console.log(something.getItems()); // []위와 같이 배열을 반환할 때 복사해서 반환하게 되면 아무리 반환된 값을 고치더라도 내부 데이터를 건드릴 수 없게 돼요. 이와 비슷하게 흔히 사용하는 방식중에 객체를 그대로 외부에서 사용할 수 있도록 할 때가 있는데요.
export const defaultOwnerData = {
firstName: "Martin",
lastName: "Fowler"
};사실 더 안전하게 하려면 다음과 같이 해야합니다.
const �ownerData = {
firstName: "Martin",
lastName: "Fowler"
};
export const defaultOwnerData = { ...ownerData };그러면 외부에서 값을 변경하더라도 원본 데이터는 변경되지 않습니다.
Author
There was a problem hiding this comment.
오 그냥 프로퍼티 접근를 반환하는 getter setter만 있으면 방어적일 수 있다고 생각했는데, 저렇게 반환해줘야하는지 몰랐네요 ㅎㅎㅎ 감사합니다
hannut91
approved these changes
Sep 4, 2023
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
선형탐색부분 구현은 너무 어려워서 이해만 했습니다...
이진탐색트리
2-3트리
레드블랙트리
개별해시테이블
선형해시테이블