You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
6
6
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.
8
8
9
-
For instance:
9
+
Ví dụ:
10
10
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.
14
14
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.
16
16
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.
18
18
19
-
## Arrow functions have no "this"
19
+
## Các hàm mũi tên không có "this"
20
20
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.
22
22
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:
24
24
25
25
```js run
26
26
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"],
29
29
30
30
showList() {
31
31
*!*
@@ -39,19 +39,19 @@ let group = {
39
39
group.showList();
40
40
```
41
41
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`.
43
43
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:
45
45
46
46
```js run
47
47
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"],
50
50
51
51
showList() {
52
52
*!*
53
53
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
55
55
alert(this.title+': '+ student)
56
56
});
57
57
*/!*
@@ -61,28 +61,28 @@ let group = {
61
61
group.showList();
62
62
```
63
63
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`.
65
65
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`.
67
67
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`.
70
70
```
71
71
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)`:
74
74
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`.
77
77
```
78
78
79
-
## Arrows have no "arguments"
79
+
## Hàm mũi tên không có "arguments"
80
80
81
-
Arrow functions also have no `arguments` variable.
81
+
Hàm mũi tên cũng không có biến `arguments`.
82
82
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`và`arguments` của hàm hiện tại.
84
84
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.
86
86
87
87
```js run
88
88
functiondefer(f, ms) {
@@ -92,14 +92,14 @@ function defer(f, ms) {
92
92
}
93
93
94
94
functionsayHi(who) {
95
-
alert('Hello, '+ who);
95
+
alert('Xin chào, '+ who);
96
96
}
97
97
98
98
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
100
100
```
101
101
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:
103
103
104
104
```js
105
105
functiondefer(f, ms) {
@@ -112,15 +112,15 @@ function defer(f, ms) {
112
112
}
113
113
```
114
114
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`và`ctx`để hàm bên trong `setTimeout`có thể nhận chúng.
116
116
117
-
## Summary
117
+
## Tóm tắt
118
118
119
-
Arrow functions:
119
+
Hàm mũi tên:
120
120
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>).
125
125
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