- Notifications
You must be signed in to change notification settings - Fork44
WeakMap and WeakSet#193
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
ImVietnam wants to merge14 commits intojavascript-tutorial:masterChoose a base branch fromImVietnam:patch-18
base:master
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.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
14 commits Select commitHold shift + click to select a range
a3e54cf Update article.md
ImVietnam1db43c8 Update task.md
ImVietnam88a3971 Update solution.md
ImVietname447117 Update task.md
ImVietnamd65a495 Update solution.md
ImVietnamc630888 Update solution.md
ImVietnam99ac332 Merge branch 'javascript-tutorial:master' into patch-18
ImVietnameb01230 Update solution.md
ImVietnamdf92063 Update task.md
ImVietnambb6fd89 Update task.md
ImVietname7a0747 Update task.md
ImVietnam813e5b5 some fixes
ImVietnam1a0b82f sync with en version
ImVietnamaa5ce67 Merge branch 'javascript-tutorial:master' into patch-18
ImVietnamFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
32 changes: 16 additions & 16 deletions1-js/05-data-types/08-weakmap-weakset/01-recipients-read/solution.md
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 |
|---|---|---|
| @@ -1,43 +1,43 @@ | ||
| Hãy lưu trữ tin nhắn đã đọc trong `WeakSet`: | ||
| ```js run | ||
| let messages = [ | ||
| {text: "Xin chào", from: "John"}, | ||
| {text: "Tiến triển thế nào rồi?", from: "John"}, | ||
| {text: "Hẹn sớm gặp lại", from: "Alice"} | ||
| ]; | ||
| let readMessages = new WeakSet(); | ||
| //hai tin nhắn đã được đọc | ||
| readMessages.add(messages[0]); | ||
| readMessages.add(messages[1]); | ||
| // readMessages has 2 elements | ||
| // ...hãy đọc lại tin nhắn đầu tiên! | ||
| readMessages.add(messages[0]); | ||
| // readMessagesvẫn có 2yếu tố độc nhất | ||
| //trả lời: messages[0]đã được đọc chưa? | ||
| alert("Read message 0: " + readMessages.has(messages[0])); // true | ||
| messages.shift(); | ||
| //bây giờreadMessagescó 1phần tử (về mặt kỹ thuật, bộ nhớ có thể được xóa sau) | ||
| ``` | ||
| `WeakSet`cho phép lưu trữ một tập hợp các thông báo và dễ dàng kiểm tra sự tồn tại của một thông báo trong đó. | ||
| Nó tự động dọn dẹp. Sự đánh đổi là chúng ta không thể lặp lại nó, không thể nhận trực tiếp "tất cả các tin nhắn đã đọc" từ nó. Nhưng chúng ta có thể làm điều đó bằng cách lặp lại tất cả các tin nhắn và lọc những tin nhắn có trong set. | ||
| Một giải pháp khác, khác có thể là thêm thuộc tính như`message.isRead=true`vào tin nhắn sau khi tin nhắn được đọc. Vì các đối tượng thông báo được quản lý bởi một mã khác, điều đó thường không được khuyến khích, nhưng chúng ta có thể sử dụng một thuộc tính tượng trưng để tránh xung đột. | ||
| Như thế này: | ||
| ```js | ||
| //thuộc tính tượng trưng chỉ được biết đến với mã của chúng ta | ||
| let isRead = Symbol("isRead"); | ||
| messages[0][isRead] = true; | ||
| ``` | ||
| Bây giờ mã của bên thứ ba có thể sẽ không thấy thuộc tính bổ sung của chúng ta. | ||
| Mặc dù các ký hiệu cho phép giảm xác suất xảy ra sự cố, nhưng sử dụng`WeakSet`sẽ tốt hơn từ quan điểm kiến trúc. |
18 changes: 9 additions & 9 deletions1-js/05-data-types/08-weakmap-weakset/01-recipients-read/task.md
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
10 changes: 5 additions & 5 deletions1-js/05-data-types/08-weakmap-weakset/02-recipients-when-read/solution.md
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 |
|---|---|---|
| @@ -1,15 +1,15 @@ | ||
| Để lưu trữ một ngày, chúng ta có thể sử dụng `WeakMap`: | ||
| ```js | ||
| let messages = [ | ||
| {text: "Xin chào", from: "John"}, | ||
| {text: "Tiến triển thế nào rồi?", from: "John"}, | ||
| {text: "Hẹn sớm gặp lại", from: "Alice"} | ||
| ]; | ||
| let readMap = new WeakMap(); | ||
| readMap.set(messages[0], new Date(2017, 1, 1)); | ||
| //Đối tượng ngày chúng ta sẽ nghiên cứu sau | ||
| ``` |
16 changes: 8 additions & 8 deletions1-js/05-data-types/08-weakmap-weakset/02-recipients-when-read/task.md
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.