Skip to content

Commit a6a17ab

Browse files
author
Phùng Hùng
authored
Merge pull request #7 from hunghedu/master
Arrow functions revisited
2 parents 142280c + 3d17fbf commit a6a17ab

File tree

1 file changed

+43
-43
lines changed
  • 1-js/06-advanced-functions/12-arrow-functions

1 file changed

+43
-43
lines changed
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
# Arrow functions revisited
1+
# Nhắc lại về hàm mũi tên
22

3-
Let's revisit arrow functions.
3+
Cùng xem lại hàm mũi tên.
44

5-
Arrow functions are not just a "shorthand" for writing small stuff.
5+
Hàm mũi tên không đơn giản chỉ là một cách viết tắt của hàm.
66

7-
JavaScript is full of situations where we need to write a small function, that's executed somewhere else.
7+
JavaScript có đầy những tình huống ở đó chúng ta cần viết một hàm nhỏ để chạy ở một nơi khác.
88

9-
For instance:
9+
Ví dụ:
1010

11-
- `arr.forEach(func)` -- `func` is executed by `forEach` for every array item.
12-
- `setTimeout(func)` -- `func` is executed by the built-in scheduler.
13-
- ...there are more.
11+
- `arr.forEach(func)` -- `func` được chạy bởi `forEach` với mỗi phần tử của mảng.
12+
- `setTimeout(func)` -- `func` được chạy bởi bộ lập lịch.
13+
- ...và nhiều nữa.
1414

15-
It's in the very spirit of JavaScript to create a function and pass it somewhere.
15+
Nó cũng là linh hồn của JavaScript khi mà ta cần tạo một hàm và truyền nó tới một nơi khác.
1616

17-
And in such functions we usually don't want to leave the current context.
17+
Những hàm này tạo ra để chạy ở chỗ khác cho nên nó không cần đến ngữ cảnh nơi nó được tạo.
1818

19-
## Arrow functions have no "this"
19+
## Các hàm mũi tên không có "this"
2020

21-
As we remember from the chapter <info:object-methods>, arrow functions do not have `this`. If `this` is accessed, it is taken from the outside.
21+
Như ta đã nói ở bài <info:object-methods>, hàm mũi tên không có `this`. Nếu `this` được truy cập nó được lấy từ bên ngoài.
2222

23-
For instance, we can use it to iterate inside an object method:
23+
Ví dụ, chúng ta có thể sử dụng nó trong vòng lặp bên trong phương thức của một đối tượng:
2424

2525
```js run
2626
let group = {
27-
title: "Our Group",
28-
students: ["John", "Pete", "Alice"],
27+
title: "Nhóm của chúng tôi",
28+
students: ["Hùng", "Mạnh", "Ngọc"],
2929

3030
showList() {
3131
*!*
@@ -39,19 +39,19 @@ let group = {
3939
group.showList();
4040
```
4141

42-
Here in `forEach`, the arrow function is used, so `this.title` in it is exactly the same as in the outer method `showList`. That is: `group.title`.
42+
Hàm mũi tên được sử dụng trong `forEach`, `this.title` của nó giống như của phương thức ngoài `showList`. Đó là: `group.title`.
4343

44-
If we used a "regular" function, there would be an error:
44+
Nếu sử dụng hàm bình thường, sẽ có lỗi:
4545

4646
```js run
4747
let group = {
48-
title: "Our Group",
49-
students: ["John", "Pete", "Alice"],
48+
title: "Nhóm của chúng tôi",
49+
students: ["Hùng", "Mạnh", "Ngọc"],
5050

5151
showList() {
5252
*!*
5353
this.students.forEach(function(student) {
54-
// Error: Cannot read property 'title' of undefined
54+
// Lỗi: Không thể truy cập thuộc tính 'title' của undefined
5555
alert(this.title + ': ' + student)
5656
});
5757
*/!*
@@ -61,28 +61,28 @@ let group = {
6161
group.showList();
6262
```
6363

64-
The error occurs because `forEach` runs functions with `this=undefined` by default, so the attempt to access `undefined.title` is made.
64+
Lỗi này xuất hiện bởi `forEach` chạy hàm với `this=undefined`, dẫn tới việc truy cập `undefined.title`.
6565

66-
That doesn't affect arrow functions, because they just don't have `this`.
66+
Điều này không sảy ra với hàm mũi tên vì nó không có `this`.
6767

68-
```warn header="Arrow functions can't run with `new`"
69-
Not having `this` naturally means another limitation: arrow functions can't be used as constructors. They can't be called with `new`.
68+
```warn header="Hãm mũi tên không thể chạy với `new`"
69+
Bởi không có `this`, hàm mũi tên không thể sử dụng làm constructor và do đó không thể chạy với toán tử `new`.
7070
```
7171
72-
```smart header="Arrow functions VS bind"
73-
There's a subtle difference between an arrow function `=>` and a regular function called with `.bind(this)`:
72+
```smart header="Hàm mũi tên khi sử dụng với `bind`"
73+
Có một sự khác biệt nhỏ giữa hàm mũi tên `=>` và hàm thông thường khi sử dụng `.bind(this)`:
7474
75-
- `.bind(this)` creates a "bound version" of the function.
76-
- The arrow `=>` doesn't create any binding. The function simply doesn't have `this`. The lookup of `this` is made exactly the same way as a regular variable search: in the outer lexical environment.
75+
- `.bind(this)` tạo "phiên bản ràng buộc `this`" của hàm thông thường.
76+
- Hàm mũi tên `=>` không tạo ra bất cứ ràng buộc nào. Đơn giản là bởi vì nó không có `this`.
7777
```
7878

79-
## Arrows have no "arguments"
79+
## Hàm mũi tên không có "arguments"
8080

81-
Arrow functions also have no `arguments` variable.
81+
Hàm mũi tên cũng không có biến `arguments`.
8282

83-
That's great for decorators, when we need to forward a call with the current `this` and `arguments`.
83+
Nó rất tối ưu cho các decorator, khi chúng ta cần chuyển lời gọi hàm cùng với `this` `arguments` của hàm hiện tại.
8484

85-
For instance, `defer(f, ms)` gets a function and returns a wrapper around it that delays the call by `ms` milliseconds:
85+
Ví dụ, `defer(f, ms)` nhận hàm `f` và trả về hàm bao làm trễ việc chạy `f` đi `ms` mi-li-giây.
8686

8787
```js run
8888
function defer(f, ms) {
@@ -92,14 +92,14 @@ function defer(f, ms) {
9292
}
9393

9494
function sayHi(who) {
95-
alert('Hello, ' + who);
95+
alert('Xin chào, ' + who);
9696
}
9797

9898
let sayHiDeferred = defer(sayHi, 2000);
99-
sayHiDeferred("John"); // Hello, John after 2 seconds
99+
sayHiDeferred("Hùng"); // Xin chào, Hùng sau 2 giây
100100
```
101101

102-
The same without an arrow function would look like:
102+
Nếu không dùng hàm mũi tên thì phải viết như sau:
103103

104104
```js
105105
function defer(f, ms) {
@@ -112,15 +112,15 @@ function defer(f, ms) {
112112
}
113113
```
114114

115-
Here we had to create additional variables `args` and `ctx` so that the function inside `setTimeout` could take them.
115+
Ta cần tạo thêm hai biến `args` `ctx` để hàm bên trong `setTimeout` có thể nhận chúng.
116116

117-
## Summary
117+
## Tóm tắt
118118

119-
Arrow functions:
119+
Hàm mũi tên:
120120

121-
- Do not have `this`.
122-
- Do not have `arguments`.
123-
- Can't be called with `new`.
124-
- (They also don't have `super`, but we didn't study it. Will be in the chapter <info:class-inheritance>).
121+
- Không có `this`.
122+
- Không có `arguments`.
123+
- Không thể gọi bằng `new`.
124+
- (Chúng cũng không có `super`, nhưng chúng ta chưa học. Sẽ học ở bài <info:class-inheritance>).
125125

126-
That's because they are meant for short pieces of code that do not have their own "context", but rather works in the current one. And they really shine in that use case.
126+
Đó là bởi vì hàm mũi tên là một hàm ngắn được tạo ra với mục địch chạy ở bất cứ đâu, nên nó không cần giữ lại ngữ cảnh nơi nó được tạo.

0 commit comments

Comments
 (0)