Skip to content

Commit 70f216e

Browse files
authored
Merge pull request #537 from cyz980908/Arrow-functions,-the-basics
Arrow functions, the basics
2 parents 71adf99 + 87f42d6 commit 70f216e

File tree

1 file changed

+32
-32
lines changed
  • 1-js/02-first-steps/16-arrow-functions-basics

1 file changed

+32
-32
lines changed
+32-32
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
# Arrow functions, the basics
1+
# 箭头函数,基础知识
22

3-
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
3+
创建函数还有另外一种非常简单的语法,并且这种方法通常比函数表达式更好。
44

5-
It's called "arrow functions", because it looks like this:
5+
它被称为“箭头函数”,因为它看起来像这样:
66

77
```js
88
let func = (arg1, arg2, ...argN) => expression
99
```
1010

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.
11+
……这里创建了一个函数 `func`,它接受参数 `arg1..argN`,然后使用参数对右侧的 `expression` 求值并返回其结果。
1212

13-
In other words, it's the shorter version of:
13+
换句话说,它是下面这段代码的更短的版本:
1414

1515
```js
1616
let func = function(arg1, arg2, ...argN) {
1717
return expression;
1818
};
1919
```
2020

21-
Let's see a concrete example:
21+
让我们来看一个具体的例子:
2222

2323
```js run
2424
let sum = (a, b) => a + b;
2525

26-
/* This arrow function is a shorter form of:
26+
/* 这个箭头函数是下面这个函数的更短的版本:
2727
2828
let sum = function(a, b) {
2929
return a + b;
@@ -33,32 +33,32 @@ let sum = function(a, b) {
3333
alert( sum(1, 2) ); // 3
3434
```
3535

36-
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` 求值,并返回计算结果。
3737

38-
- If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
38+
- 如果我们只有一个参数,还可以省略掉参数外的圆括号,使代码更短。
3939

40-
For example:
40+
例如:
4141

4242
```js run
4343
*!*
4444
let double = n => n * 2;
45-
// roughly the same as: let double = function(n) { return n * 2 }
45+
// 差不多等同于:let double = function(n) { return n * 2 }
4646
*/!*
4747

4848
alert( double(3) ); // 6
4949
```
5050

51-
- If there are no arguments, parentheses will be empty (but they should be present):
51+
- 如果没有参数,括号将是空的(但括号应该保留):
5252

5353
```js run
5454
let sayHi = () => alert("Hello!");
5555
5656
sayHi();
5757
```
5858

59-
Arrow functions can be used in the same way as Function Expressions.
59+
箭头函数可以像函数表达式一样使用。
6060

61-
For instance, to dynamically create a function:
61+
例如,动态创建一个函数:
6262

6363
```js run
6464
let age = prompt("What is your age?", 18);
@@ -67,45 +67,45 @@ let welcome = (age < 18) ?
6767
() => alert('Hello') :
6868
() => alert("Greetings!");
6969
70-
welcome(); // ok now
70+
welcome(); // 现在好了
7171
```
7272

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+
一开始,箭头函数可能看起来并不熟悉,也不容易读懂,但一旦我们看习惯了之后,这种情况很快就会改变。
7474

75-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
75+
箭头函数对于简单的单行动作来说非常方便,尤其是当我们懒得打太多字的时候。
7676

77-
## Multiline arrow functions
77+
## 多行的箭头函数
7878

79-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
79+
上面的例子从 `=>` 的左侧获取参数,然后使用参数计算右侧表达式的值。
8080

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.
81+
但有时我们需要更复杂一点的东西,比如多行的表达式或语句。这也是可以做到的,但是我们应该用花括号括起来。然后使用一个普通的 `return` 将需要返回的值进行返回。
8282

83-
Like this:
83+
就像这样:
8484

8585
```js run
86-
let sum = (a, b) => { // the curly brace opens a multiline function
86+
let sum = (a, b) => { // 花括号表示开始一个多行函数
8787
let result = a + b;
8888
*!*
89-
return result; // if we use curly braces, then we need an explicit "return"
89+
return result; // 如果我们使用了花括号,那么我们需要一个显式的 “return
9090
*/!*
9191
};
9292
9393
alert( sum(1, 2) ); // 3
9494
```
9595

96-
```smart header="More to come"
97-
Here we praised arrow functions for brevity. But that's not all!
96+
```smart header="更多内容"
97+
在这里,我们赞扬了箭头功能的简洁性。但还不止这些!
9898
99-
Arrow functions have other interesting features.
99+
箭头函数还有其他有趣的特性。
100100
101-
To study them in-depth, we first need to get to know some other aspects of JavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
101+
为了更深入地学习它们,我们首先需要了解一些 JavaScript 其他方面的知识,因此我们将在后面的 <info:arrow-functions> 一章中再继续研究箭头函数。
102102
103-
For now, we can already use arrow functions for one-line actions and callbacks.
103+
现在,我们已经可以用箭头函数进行单行操作和回调了。
104104
```
105105

106-
## Summary
106+
## 总结
107107

108-
Arrow functions are handy for one-liners. They come in two flavors:
108+
对于一行代码的函数来说,箭头函数是相当方便的。它具体有两种:
109109

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.
110+
1. 不带花括号:`(...args) => expression` — 右侧是一个表达式:函数计算表达式并返回其结果。
111+
2. 带花括号:`(...args) => { body }` — 花括号允许我们在函数中编写多个语句,但是我们需要显式地 `return` 来返回一些内容。

0 commit comments

Comments
 (0)