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
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
3
+
创建函数还有另外一种非常简单的语法,并且这种方法通常比函数表达式更好。
4
4
5
-
It's called "arrow functions", because it looks like this:
5
+
它被称为“箭头函数”,因为它看起来像这样:
6
6
7
7
```js
8
8
letfunc= (arg1, arg2, ...argN) => expression
9
9
```
10
10
11
-
...This creates a function `func` that accepts arguments `arg1..argN`, then evaluates the `expression`on the right side with their use and returns its result.
As you can, see `(a, b) => a + b`means a function that accepts two arguments named `a`and`b`. Upon the execution, it evaluates the expression `a + b`and returns the result.
36
+
可以看到 `(a, b) => a + b`表示一个函数接受两个名为 `a`和`b` 的参数。在执行时,它将对表达式 `a + b`求值,并返回计算结果。
37
37
38
-
-If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
38
+
-如果我们只有一个参数,还可以省略掉参数外的圆括号,使代码更短。
39
39
40
-
For example:
40
+
例如:
41
41
42
42
```js run
43
43
*!*
44
44
letdouble=n=> n *2;
45
-
//roughly the same as: let double = function(n) { return n * 2 }
45
+
//差不多等同于:let double = function(n) { return n * 2 }
46
46
*/!*
47
47
48
48
alert( double(3) ); // 6
49
49
```
50
50
51
-
-If there are no arguments, parentheses will be empty (but they should be present):
51
+
-如果没有参数,括号将是空的(但括号应该保留):
52
52
53
53
```js run
54
54
let sayHi = () => alert("Hello!");
55
55
56
56
sayHi();
57
57
```
58
58
59
-
Arrow functions can be used in the same way as Function Expressions.
59
+
箭头函数可以像函数表达式一样使用。
60
60
61
-
For instance, to dynamically create a function:
61
+
例如,动态创建一个函数:
62
62
63
63
```js run
64
64
let age = prompt("What is your age?", 18);
@@ -67,45 +67,45 @@ let welcome = (age < 18) ?
67
67
() => alert('Hello') :
68
68
() => alert("Greetings!");
69
69
70
-
welcome(); // ok now
70
+
welcome(); // 现在好了
71
71
```
72
72
73
-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
73
+
一开始,箭头函数可能看起来并不熟悉,也不容易读懂,但一旦我们看习惯了之后,这种情况很快就会改变。
74
74
75
-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
75
+
箭头函数对于简单的单行动作来说非常方便,尤其是当我们懒得打太多字的时候。
76
76
77
-
## Multiline arrow functions
77
+
## 多行的箭头函数
78
78
79
-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
79
+
上面的例子从 `=>`的左侧获取参数,然后使用参数计算右侧表达式的值。
80
80
81
-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
let sum = (a, b) => { // the curly brace opens a multiline function
86
+
let sum = (a, b) => { // 花括号表示开始一个多行函数
87
87
let result = a + b;
88
88
*!*
89
-
return result; // if we use curly braces, then we need an explicit "return"
89
+
return result; // 如果我们使用了花括号,那么我们需要一个显式的 “return”
90
90
*/!*
91
91
};
92
92
93
93
alert( sum(1, 2) ); // 3
94
94
```
95
95
96
-
```smart header="More to come"
97
-
Here we praised arrow functions for brevity. But that's not all!
96
+
```smart header="更多内容"
97
+
在这里,我们赞扬了箭头功能的简洁性。但还不止这些!
98
98
99
-
Arrow functions have other interesting features.
99
+
箭头函数还有其他有趣的特性。
100
100
101
-
To study them in-depth, we first need to get to know some other aspects ofJavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
For now, we can already use arrow functions for one-line actions and callbacks.
103
+
现在,我们已经可以用箭头函数进行单行操作和回调了。
104
104
```
105
105
106
-
## Summary
106
+
## 总结
107
107
108
-
Arrow functions are handy for one-liners. They come in two flavors:
108
+
对于一行代码的函数来说,箭头函数是相当方便的。它具体有两种:
109
109
110
-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
111
-
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.
0 commit comments