Skip to content

Commit 2a31ee0

Browse files
author
Phùng Hùng
committed
Translate Prototypal inheritance into Vietnamese
1 parent 2e52fc2 commit 2a31ee0

File tree

9 files changed

+134
-134
lines changed

9 files changed

+134
-134
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
1. `true`, taken from `rabbit`.
3-
2. `null`, taken from `animal`.
4-
3. `undefined`, there's no such property any more.
2+
1. `true`, lấy từ `rabbit`.
3+
2. `null`, lấy từ `animal`.
4+
3. `undefined`, lấy từ `Object.prototype` nhưng nó không có thuộc tính `jumps`.

1-js/08-prototypes/01-prototype-inheritance/1-property-after-delete/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Working with prototype
5+
# Làm việc với nguyên mẫu
66

7-
Here's the code that creates a pair of objects, then modifies them.
7+
Đây là mã tạo một cặp đối tượng, sau đó sửa chúng.
88

9-
Which values are shown in the process?
9+
Giá tri nào sẽ được hiển thị?
1010

1111
```js
1212
let animal = {
@@ -28,4 +28,4 @@ delete animal.jumps;
2828
alert( rabbit.jumps ); // ? (3)
2929
```
3030

31-
There should be 3 answers.
31+
Sẽ có 3 câu trả lời.

1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
1. Let's add `__proto__`:
2+
1. Thêm `__proto__` như sau:
33

44
```js run
55
let head = {
@@ -27,6 +27,6 @@
2727
alert( table.money ); // undefined
2828
```
2929

30-
2. In modern engines, performance-wise, there's no difference whether we take a property from an object or its prototype. They remember where the property was found and reuse it in the next request.
30+
2. Trong các JavaScript engine hiện đại, hiệu năng rất tốt, không có khác biệt nào giữa việc lấy thuộc tính từ đối tượng hay từ nguyên mẫu. Chúng nhớ nơi thuộc tính được tìm thấy để tái sử dụng trong những lần sau.
3131

32-
For instance, for `pockets.glasses` they remember where they found `glasses` (in `head`), and next time will search right there. They are also smart enough to update internal caches if something changes, so that optimization is safe.
32+
Ví dụ, với `pockets.glasses` engine nhớ nơi nó tìm thấy `glasses` (trong `head`), và trong lần sau sẽ tìm ngay trong đó. Nó cũng đủ thông minh để cập nhật lại nếu có gì đó thay đổi, để việc tối ưu hóa vẫn an toàn.

1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Searching algorithm
5+
# Thuật toán tìm thuộc tính
66

7-
The task has two parts.
7+
Bài tập có hai phần.
88

9-
We have an object:
9+
Chúng ta có một đối tượng:
1010

1111
```js
1212
let head = {
@@ -27,5 +27,5 @@ let pockets = {
2727
};
2828
```
2929

30-
1. Use `__proto__` to assign prototypes in a way that any property lookup will follow the path: `pockets` -> `bed` -> `table` -> `head`. For instance, `pockets.pen` should be `3` (found in `table`), and `bed.glasses` should be `1` (found in `head`).
31-
2. Answer the question: is it faster to get `glasses` as `pockets.glasses` or `head.glasses`? Benchmark if needed.
30+
1. Sử dụng `__proto__` để tạo chuỗi nguyên mẫu, sao cho bất kỳ thuộc tính nào đều được tìm theo thứ tự: `pockets` -> `bed` -> `table` -> `head`. Ví dụ, `pockets.pen` `3` (tìm thấy trong `table`), `bed.glasses` `1` (tìm thấy trong `head`).
31+
2. Trả lời câu hỏi: có thể lấy `glasses` bằng `pockets.glasses` hoặc `head.glasses`, cách nào nhanh hơn?
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
**The answer: `rabbit`.**
1+
**Trả lời: `rabbit`.**
22

3-
That's because `this` is an object before the dot, so `rabbit.eat()` modifies `rabbit`.
3+
Bởi vì `this` là đối tượng trước dấu chấm, nên `rabbit.eat()` thay đổi `rabbit`.
44

5-
Property lookup and execution are two different things.
6-
The method `rabbit.eat` is first found in the prototype, then executed with `this=rabbit`
5+
Tìm thuộc tính và chạy thuộc tính là hai việc khác nhau.
6+
Phương thức `rabbit.eat` tìm thấy trong nguyên mẫu, nhưng lại chạy trong `rabbit`.

1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Where it writes?
5+
# Dữ liệu ghi vào đối tượng nào?
66

7-
We have `rabbit` inheriting from `animal`.
7+
Chúng ta có `rabbit` thừa kế từ `animal`.
88

9-
If we call `rabbit.eat()`, which object receives the `full` property: `animal` or `rabbit`?
9+
Nếu chúng ta gọi `rabbit.eat()`, đối tượng nào nhận được thuộc tính `full`: `animal` hay `rabbit`?
1010

1111
```js
1212
let animal = {

1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
Let's look carefully at what's going on in the call `speedy.eat("apple")`.
1+
Cùng xem xét cẩn thận chuyện xảy ra khi gọi `speedy.eat("apple")`.
22

3-
1. The method `speedy.eat` is found in the prototype (`=hamster`), then executed with `this=speedy` (the object before the dot).
3+
1. Phương thức `speedy.eat` tìm thấy trong nguyên mẫu (`=hamster`), sau đó chạy trong `speedy` (`this=speedy`).
44

5-
2. Then `this.stomach.push()` needs to find `stomach` property and call `push` on it. It looks for `stomach` in `this` (`=speedy`), but nothing found.
5+
2. Tiếp theo `this.stomach.push()` cần tìm thuộc tính `stomach` và gọi phương thức `push` từ đó. Trước tiên `stomach` được tìm trong `this` (`=speedy`), nhưng không thấy.
66

7-
3. Then it follows the prototype chain and finds `stomach` in `hamster`.
7+
3. Sau đó `stomach` được tìm thấy trong nguyên mẫu `hamster` từ giờ `this.stomach` `hamster.stomach`.
88

9-
4. Then it calls `push` on it, adding the food into *the stomach of the prototype*.
9+
4. Khi gọi `this.stomach.push` thì `"apple"` được thêm vào `hamster.stomach`.
1010

11-
So all hamsters share a single stomach!
11+
Vậy nên cả hai con hamsters đều dùng chung `hamster.stomach` - tức có cùng một cái dạ dày! Khi một con no, con kia cũng no là vì thế.
1212

13-
Every time the `stomach` is taken from the prototype, then `stomach.push` modifies it "at place".
13+
Mỗi khi `stomach` được lấy từ nguyên mẫu, thì `this.stomach.push` lại làm thay đổi `hamster.stomach`.
1414

15-
Please note that such thing doesn't happen in case of a simple assignment `this.stomach=`:
15+
Chú ý rằng chuyện này không xảy ra nếu ta gán `this.stomach=`:
1616

1717
```js run
1818
let hamster = {
1919
stomach: [],
2020

2121
eat(food) {
2222
*!*
23-
// assign to this.stomach instead of this.stomach.push
23+
// gán tới to this.stomach thay vì this.stomach.push
2424
this.stomach = [food];
2525
*/!*
2626
}
@@ -34,17 +34,17 @@ let lazy = {
3434
__proto__: hamster
3535
};
3636

37-
// Speedy one found the food
37+
// Speedy ăn táo
3838
speedy.eat("apple");
3939
alert( speedy.stomach ); // apple
4040

41-
// Lazy one's stomach is empty
42-
alert( lazy.stomach ); // <nothing>
41+
// Lazy không được ăn theo
42+
alert( lazy.stomach ); // <không có gì>
4343
```
4444

45-
Now all works fine, because `this.stomach=` does not perform a lookup of `stomach`. The value is written directly into `this` object.
45+
Giờ tất cả làm việc, vì `this.stomach=` là hành động ghi nên không sử dụng thuộc tính `stomach` của nguyên mẫu `hamster`. Giá trị được ghi vào đối tượng `this` (tức đối tượng được thừa kế).
4646

47-
Also we can totally evade the problem by making sure that each hamster has their own stomach:
47+
Ta cũng có thể tránh được vấn đề trên bằng cách tạo riêng cho mỗi con hamster một cái dạ dày:
4848

4949
```js run
5050
let hamster = {
@@ -69,12 +69,12 @@ let lazy = {
6969
*/!*
7070
};
7171

72-
// Speedy one found the food
72+
// Speedy ăn táo
7373
speedy.eat("apple");
7474
alert( speedy.stomach ); // apple
7575

76-
// Lazy one's stomach is empty
77-
alert( lazy.stomach ); // <nothing>
76+
// Dạ dày của Lazy vẫn trống
77+
alert( lazy.stomach ); // <không có gì>
7878
```
7979

80-
As a common solution, all properties that describe the state of a particular object, like `stomach` above, are usually written into that object. That prevents such problems.
80+
Đây là giải pháp tổng quát, các thuộc tính mô tả trạng thái của đối tượng, giống `stomach` ở trên nên được ghi riêng vào đối tượng đó, không nên dùng chung từ một nguyên mẫu. Điều này giúp ta không gặp phải vấn đề trên.

1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Why two hamsters are full?
5+
# Tại sao cả hai con hamster đều no?
66

7-
We have two hamsters: `speedy` and `lazy` inheriting from the general `hamster` object.
7+
Ta có hai con hamster: `speedy` `lazy` đều thừa kế từ đối tượng `hamster`.
88

9-
When we feed one of them, the other one is also full. Why? How to fix it?
9+
Khi chúng ta cho một trong số chúng ăn no, con kia cũng no. Tại sao? Sửa lại như thế nào?
1010

1111
```js run
1212
let hamster = {
@@ -25,11 +25,11 @@ let lazy = {
2525
__proto__: hamster
2626
};
2727

28-
// This one found the food
28+
// Cho một con ăn no
2929
speedy.eat("apple");
3030
alert( speedy.stomach ); // apple
3131

32-
// This one also has it, why? fix please.
32+
// Con kia cũng vậy! Tại sao? Hãy sửa lại?
3333
alert( lazy.stomach ); // apple
3434
```
3535

0 commit comments

Comments
 (0)