Skip to content

Commit 089b1f5

Browse files
committed
i18n: translate all qns
1 parent dc83f48 commit 089b1f5

File tree

386 files changed

+31970
-525
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

386 files changed

+31970
-525
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"frontmatter": {
3+
"title": "5ba0d4da",
4+
"subtitle": "4d47ead"
5+
},
6+
"content": {
7+
"source": {
8+
"locale": "en-US",
9+
"hashes": [
10+
"77da26a5",
11+
"4010c03b",
12+
"b7950cbe",
13+
"2a7816d0",
14+
"678d2642",
15+
"7ac0c956",
16+
"4e1ff55c",
17+
"d3b7242",
18+
"a203d791",
19+
"8550812",
20+
"54dee6b3",
21+
"a6f52eea",
22+
"cb0ae1a8",
23+
"e5dcf6c0",
24+
"839f0ebb",
25+
"d8fa43b8",
26+
"30eae852",
27+
"be10dd6f",
28+
"1f4770e5",
29+
"6232231"
30+
]
31+
},
32+
"targets": {
33+
"zh-CN": [
34+
"77da26a5",
35+
"4010c03b",
36+
"b7950cbe",
37+
"2a7816d0",
38+
"678d2642",
39+
"7ac0c956",
40+
"4e1ff55c",
41+
"d3b7242",
42+
"a203d791",
43+
"8550812",
44+
"54dee6b3",
45+
"a6f52eea",
46+
"cb0ae1a8",
47+
"e5dcf6c0",
48+
"839f0ebb",
49+
"d8fa43b8",
50+
"30eae852",
51+
"be10dd6f",
52+
"1f4770e5",
53+
"6232231"
54+
]
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,102 @@
11
---
2-
title: 您可以为新箭头函数语法提供一个用例吗
3-
subtitle: 这个新语法与其他函数有什么不同
2+
title: 箭头 => 函数语法有什么用例
3+
subtitle: 这种新语法与其他函数有何不同
44
---
55

6-
`=>` 函数语法,也被称为 "箭头函数",是在 ECMAScript 6(ES6)中引入的 JavaScript 功能。它提供了一种更简洁的编写函数的方式,并且在 `this` 的行为方面与传统函数表达式有一些不同。下面通过一个简单的用例来说明它的实用性:
6+
## TL;DR
77

8-
## 用例:筛选数组
8+
箭头函数为用 JavaScript 编写函数提供了一种简洁的语法。它们对于在方法和回调中维护 `this` 上下文特别有用。例如,在事件处理程序或像 `map` 这样的数组方法中,箭头函数可以简化代码并避免 `this` 绑定问题。
99

10-
假设你有一个包含数字的数组,你想筛选出所有小于 10 的数字。你可以使用传统的函数语法和新的箭头函数语法来实现这一目标,如下所示:
10+
```js live
11+
const numbers = [1, 2, 3];
12+
const doubled = numbers.map((n) => n * 2);
13+
console.log(doubled); // [2, 4, 6]
14+
```
1115

12-
### 传统函数语法
16+
***
1317

14-
```js
15-
const numbers = [1, 5, 10, 15, 20];
16-
const filteredNumbers = numbers.filter(function (number) {
17-
return number >= 10;
18-
});
18+
## 新箭头 => 函数语法的用例
19+
20+
### 简化语法
21+
22+
箭头函数提供了一种更简洁的编写函数的方式。这对于短函数或回调特别有用。
1923

20-
console.log(filteredNumbers); // 输出: [10, 15, 20]
24+
```js live
25+
// Traditional function
26+
const add = function (a, b) {
27+
return a + b;
28+
};
29+
30+
// Arrow function
31+
const anotherAdd = (a, b) => a + b;
32+
33+
console.log(add(2, 3)); // Output: 5
34+
console.log(anotherAdd(2, 3)); // Output: 5
2135
```
2236

23-
### 箭头函数语法
37+
### 词法 `this` 绑定
38+
39+
箭头函数没有自己的 `this` 上下文。相反,它们从周围的范围继承 `this`。这在 `this` 上下文可能很棘手的方法和回调中特别有用。
2440

25-
```js
26-
const numbers = [1, 5, 10, 15, 20];
27-
const filteredNumbers = numbers.filter((number) => number >= 10);
41+
```js live
42+
function Timer() {
43+
this.seconds = 0;
44+
this.increment = () => {
45+
this.seconds++; // 'this.seconds' is inherited from the outer scope
46+
console.log(this.seconds);
47+
};
48+
}
2849

29-
console.log(filteredNumbers); // 输出: [10, 15, 20]
50+
const timer = new Timer();
51+
timer.increment(); // 1
52+
timer.increment(); // 2
3053
```
3154

32-
## 箭头函数的优点
55+
在上面的例子中,在 `setInterval` 中使用传统函数需要额外的步骤来维护正确的 `this` 上下文。
3356

34-
1. **简洁性**:箭头函数更加简洁,使你的代码更短且易于阅读。
35-
1. **`this` 行为**:箭头函数没有自己的 `this`。相反,它们继承自它们在定义时的父级作用域中的 `this`。这在处理回调并希望保留 `this` 上下文的情况下特别有用。
36-
1. **隐式返回**:如果函数体只包含一个表达式,箭头函数允许省略 `return` 关键字和花括号。
57+
### 在数组方法中使用箭头函数
3758

38-
## 何时使用箭头函数
59+
箭头函数通常用于 `map``filter``reduce` 等数组方法中,以获得更简洁、更易读的代码。
3960

40-
- 当你需要一个快速的、单行的函数时。
41-
- 在回调函数中,当你想要保留 `this` 的词法作用域时。
42-
- 在使用 `map``filter``reduce` 等高阶函数时。
61+
```js live
62+
const numbers = [1, 2, 3, 4, 5];
4363

44-
## 何时不要使用箭头函数
64+
// Traditional function
65+
const doubledTraditional = numbers.map(function (n) {
66+
return n * 2;
67+
});
68+
69+
// Arrow function
70+
const doubled = numbers.map((n) => n * 2);
71+
72+
console.log(doubled); // [2, 4, 6, 8, 10]
73+
```
74+
75+
### 事件处理程序
76+
77+
箭头函数可用于事件处理程序,以维护类或对象的 `this` 上下文。
78+
79+
```js live
80+
class Button {
81+
constructor() {
82+
this.count = 0;
83+
this.button = document.createElement('button');
84+
this.button.innerText = 'Click me';
85+
this.button.addEventListener('click', () => {
86+
this.count++;
87+
console.log('count:', this.count);
88+
});
89+
document.body.appendChild(this.button);
90+
}
91+
}
92+
93+
const myButton = new Button();
94+
myButton.button.click(); // count: 1
95+
myButton.button.click(); // count: 2
96+
```
4597

46-
- **对象方法**:箭头函数没有自己的 `this` 上下文,在对象方法中可能会导致意外行为。
47-
- **作为构造函数**:箭头函数不能作为构造函数使用,如果使用 `new` 关键字会引发错误。
48-
- **当你需要使用函数提升**:与传统的函数声明不同,箭头函数不会提升。
98+
## 延伸阅读
4999

50-
箭头函数是 JavaScript 的一个强大补充,简化了函数语法,并解决了传统函数中 `this` 关键字的一些常见问题。它们在现代 JavaScript 开发模式中特别有用。
100+
* [MDN Web Docs: Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
101+
* [JavaScript.info: Arrow functions revisited](https://javascript.info/arrow-functions)
102+
* [Eloquent JavaScript: Functions](https://eloquentjavascript.net/03_functions.html)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"frontmatter": {
3+
"title": "e76fac09"
4+
},
5+
"content": {
6+
"source": {
7+
"locale": "en-US",
8+
"hashes": [
9+
"77da26a5",
10+
"1cdd9fe1",
11+
"940c1a5e",
12+
"2a7816d0",
13+
"8f8a1b0c",
14+
"72e7e3c9",
15+
"63bf7424",
16+
"a71258b2",
17+
"7009889d",
18+
"7c18e66f",
19+
"ae34ee20",
20+
"e04e0f0f",
21+
"d186cc4a",
22+
"a517a702",
23+
"9e538dcd",
24+
"3818abba",
25+
"47a90b06",
26+
"10d6cb7a",
27+
"1f177e65",
28+
"d7a0aa2",
29+
"68b07c90",
30+
"e2a961ac",
31+
"c81ac723",
32+
"965b6bd7",
33+
"b0d83e27",
34+
"a8ff50ea",
35+
"3625d327",
36+
"ac7b2126",
37+
"92f262c4",
38+
"b37f85fb",
39+
"3c0717ad",
40+
"26112098",
41+
"2dfccf1a",
42+
"5a641ee3",
43+
"647830d7",
44+
"758c287c",
45+
"ad083d17",
46+
"da4b57bd",
47+
"1f4770e5",
48+
"d00fe19e"
49+
]
50+
},
51+
"targets": {
52+
"zh-CN": [
53+
"77da26a5",
54+
"1cdd9fe1",
55+
"940c1a5e",
56+
"2a7816d0",
57+
"8f8a1b0c",
58+
"72e7e3c9",
59+
"63bf7424",
60+
"a71258b2",
61+
"7009889d",
62+
"7c18e66f",
63+
"ae34ee20",
64+
"e04e0f0f",
65+
"d186cc4a",
66+
"a517a702",
67+
"9e538dcd",
68+
"3818abba",
69+
"47a90b06",
70+
"10d6cb7a",
71+
"1f177e65",
72+
"d7a0aa2",
73+
"68b07c90",
74+
"e2a961ac",
75+
"c81ac723",
76+
"965b6bd7",
77+
"b0d83e27",
78+
"a8ff50ea",
79+
"3625d327",
80+
"ac7b2126",
81+
"92f262c4",
82+
"b37f85fb",
83+
"3c0717ad",
84+
"26112098",
85+
"2dfccf1a",
86+
"5a641ee3",
87+
"647830d7",
88+
"758c287c",
89+
"ad083d17",
90+
"da4b57bd",
91+
"1f4770e5",
92+
"d00fe19e"
93+
]
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)