GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Nullish coalescing operator '??' by hiimchinh · Pull Request #125 · javascript-tutorial/vi.javascript.info Conversation
|
There was an error while loading. Please reload this page.
| Toán tử nullish coalescing được viết dưới dạng hai dấu hỏi `??`. | ||
|
|
||
| The nullish coalescing operator is written as two question marks `??`. | ||
| Do toán tử này coi `null` và `undefined` như nhau, chúng ta sẽ dùng một thuật ngữ đặc biệt ở bài này. Chúng ta sẽ nói rằng một biểu thức được coi là "được định nghĩa" khi nó không phải là `null` hoặc `undefined`. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
null và undefined như nhau, chúng ta sẽ dùng một thuật ngữ đặc biệt ở bài này. => Câu này cung cấp thêm thông tin nhưng nó không có trong bản gốc. Liệu nên giữ nó lại hay bỏ đi?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
em thấy là ở bản gốc tiếng anh có thay đổi khác với bản hiện tại tiếng việt nên em đã dịch theo bản mới luôn ạ.
https://javascript.info/nullish-coalescing-operator
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mình đã update code mới nhất từ repo tiếng Anh sang repo tiếng Việt, do đó pull request này của bạn đã bị conflict. Bạn có thể cập nhật bản dịch này và xử lý conflict nếu muốn.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| ```js | ||
| result = (a !== null && a !== undefined) ? a : b; | ||
| result = a !== null && a !== undefined ? a : b; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Không sửa phần mã của bản gốc, trừ khi nó là chuỗi kí tự hoặc chú thích.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Giờ thì chắc bạn đã biết rõ toán tử `??` làm gì. Hãy xem có thể áp dụng toán tử này ở đâu. | ||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Đoạn này không có trong bản gốc.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ``` | ||
|
|
||
| Of course, if `user` had any value except `null/undefined`, then we would see it instead: | ||
| Còn dưới đây là ví dụ với `user` được gán với một cái tên: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Dịch không giống bản gốc.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ``` | ||
|
|
||
| We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`. | ||
| Chúng ta cũng có thể dùng nhiều toán tử `??` để chọn giá trị đầu tiên trong một danh sách mà không có giá trị là `null/undefined`. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sửa câu này lại như sau liệu có hợp lý hơn?
Chúng ta cũng có thể dùng nhiều toán tử ?? để chọn giá trị đầu tiên không phải là null/undefined trong một danh sách.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| - Còn biểu thức `height ?? 100` kiểm tra xem `height` có phải là `null/undefined` hay không, và nó không phải, | ||
| - thế nên kết quả của biểu thức là `height`, tức là `0`. | ||
|
|
||
| Trong thực tiễn, height là 0 thường là một giá trị hợp lệ và không nên thay thế bằng một giá trị mặc định. Vì thế ở đây sử dụng `??` là chính xác. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Dịch không giống bản gốc.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Thứ tự thực hiện cảu toán tử `??` gần ngang với `||`, chỉ thấp hơn một chút. Nó xếp thứ 5 trong [bảng MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), trong khi `||` xếp thứ 6. | ||
|
|
||
| The precedence of the `??` operator is rather low: `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). So `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`. | ||
| Điều đó có nghĩa là, giống như `||`, toán tử nullish coaslescing `??` sẽ được đánh giá trước `=` và `?`, nhưng sau phần lớn các toán tử khác, ví dụ như `+`, `*`. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hai đoạn này tuy không sai nhưng không giống bản gốc nên cần dịch lại để tôn trọng bản gốc.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Điều đó có nghĩa là, giống như `||`, toán tử nullish coaslescing `??` sẽ được đánh giá trước `=` và `?`, nhưng sau phần lớn các toán tử khác, ví dụ như `+`, `*`. | ||
|
|
||
| So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses: | ||
| Thế nên nếu chúng ta muốn sử dụng `??` trong một biểu thức với các toán tử khác, thì việc cho thêm ngoặc đơn nên được cân nhắc: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Trong tình huống thông thường nên sử dụng câu chủ động thì tốt hơn là câu bị động, ví dụ như:
Thế nên nếu chúng ta muốn sử dụng ?? trong một biểu thức với các toán tử khác, thì nên cân nhắc cho thêm các dấu ngoặc đơn:
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| let area = height ?? 100 * width ?? 50; | ||
|
|
||
| // ...works the same as this (probably not what we want): | ||
| // ... hoạt động tương đương như với dòng sau (có lẽ không như chúng ta mong muốn): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
hoạt động tương đương như với dòng sau
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Vì lý do an toàn, Javascript cấm sử dụng `??` chung với toán tử `&&` và `||`, trừ khi thứ tự thực hiện được chỉ rõ với ngoặc đơn. | ||
|
|
||
| The code below triggers a syntax error: | ||
| Dòng code dưới đây gây ra một syntax error (lỗi cú pháp): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Cụm từ "syntax error" trong tình huống này không phải là một thuật ngữ hoặc danh từ riêng được sử dụng nhiều lần trong bài viết, nên nó chỉ có tác dụng ghi chú về mặt dịch thuật. Cho nó vào trong cặp ngoặc đơn có lẽ hợp lý hơn:
Dòng code dưới đây gây ra một lỗi cú pháp (syntax error):
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@tunggnu Có nên đóng PR này không, outdated quá rồi, chắc bạn dịch không update đâu |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@henryonsoftware Đóng PR này lại là hợp lý. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.