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
+
Es gibt eine weitere sehr einfache und prägnante Syntax für die Erstellung von Funktionen, die oft besser ist als der Funktionsausdruck.
4
4
5
-
It's called "arrow functions", because it looks like this:
5
+
Sie werden Pfeilfunktionen genannt, weil sie so aussehen:
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.
11
+
...Dies erzeugt eine Funktion`func`, welche die Argumente`arg1..argN` akzeptiert, dann den Ausdruck`expression`auf der rechten Seite auswertet und ihr Ergebnis zurückgibt.
12
12
13
-
In other words, it's the shorter version of:
13
+
In anderen Worten, es ist die verkürzte Version von:
14
14
15
15
```js
16
16
letfunc=function(arg1, arg2, ...argN) {
17
17
return expression;
18
18
};
19
19
```
20
20
21
-
Let's see a concrete example:
21
+
Sehen wir uns ein konkretes Beispiel an:
22
22
23
23
```js run
24
24
letsum= (a, b) => a + b;
25
25
26
-
/*This arrow function is a shorter form of:
26
+
/*Diese Pfeilfunktion ist eine kürzere Form von:
27
27
28
28
let sum = function(a, b) {
29
29
return a + b;
@@ -33,79 +33,79 @@ let sum = function(a, b) {
33
33
alert( sum(1, 2) ); // 3
34
34
```
35
35
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
+
Wie man sehen kann, hat`(a, b) => a + b`die Bedeutung einer Funktion, die zwei Argumente `a` and `b` akzeptiert. Bei der Ausführung wird der Wert `a + b`ausgewertet und das Ergebnis zurückgegegeben.
37
37
38
-
-If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
38
+
-Wenn nur ein Argument vorhanden ist, können die Klammern um den Parameter wegelassen werden, was den Ausdruck noch weiter verkürzt.
39
39
40
-
For example:
40
+
Zum Beispiel:
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
+
//ungefähr dasselbe wie: 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
+
-Wenn es keine Argument gibt, sind die Klammern leer (aber sie sollte vorhanden sein):
52
52
53
53
```js run
54
-
let sayHi = () => alert("Hello!");
54
+
let sayHi = () => alert("Hallo!");
55
55
56
56
sayHi();
57
57
```
58
58
59
-
Arrow functions can be used in the same way as Function Expressions.
59
+
Pfeilfunktionen können auf die gleiche Weise wie Funktionsausdrücke verwendet werden.
60
60
61
-
For instance, to dynamically create a function:
61
+
Zum Beispiel, um eine Funktion dynamisch zu erstellen:
62
62
63
63
```js run
64
-
let age = prompt("What is your age?", 18);
64
+
let age = prompt("Wie alt bist Du?", 18);
65
65
66
66
let welcome = (age < 18) ?
67
-
() => alert('Hello') :
68
-
() => alert("Greetings!");
67
+
() => alert('Hallo') :
68
+
() => alert("Grüße!");
69
69
70
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
+
Pfeilfunktion mögen auf den ersten Blick ungewohnt und nicht sehr lesbar erscheinen, aber das ändert sich schnell, wenn sich die Augen an die Struktur gewöhnen.
74
74
75
-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
75
+
Sie sind sehr praktisch für einfache einzeilige Aktionen, wenn wir einfach zu faul sind, viele Worte zu schreiben.
76
76
77
-
## Multiline arrow functions
77
+
## Mehrzeilige Pfeilfunktionen
78
78
79
-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
79
+
Die Beispiele oben nahmen Argumente von der linken Seite`=>`und bewerteten damit den Ausdruck auf der rechten Seite.
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.
81
+
Manchmal brauchen wir etwas Komplexeres, wie mehrfache Ausdrücke oder Anweisungen. Das ist auch möglich, aber wir sollten sie ingeschweifte Klammern einschließen. Dann verwende ein normales`return`innerhalb dieser Klammern.
82
82
83
-
Like this:
83
+
Etwa so:
84
84
85
85
```js run
86
-
let sum = (a, b) => { // the curly brace opens a multiline function
86
+
let sum = (a, b) => { // die geschweifte Klammer öffnet eine mehrzeilige Funktion
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; // wenn wir geschweifte Klammern verwenden, dann brauchen wir ein explizites "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="Noch mehr später"
97
+
Wir haben Pfeilfunktionen für ihre Kürze gelobt. Aber das ist nicht alles!
98
98
99
-
Arrow functions have other interesting features.
99
+
Pfeilfunktionen haben weitere interessante Eigenschaften.
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>.
101
+
Um diese zu verstehen, müssen wir erst einige weitere Aspekte von JavaScript kennenlernen. Wir werden im Kapitel <info:arrow-functions> zu Pfeilfunktion zurückkehren.
102
102
103
-
For now, we can already use arrow functions for one-line actions and callbacks.
103
+
Für den Moment können wir Pfeilfunktionen für einzeilige Aktionen und Callback-Funktionen verwenden.
104
104
```
105
105
106
-
## Summary
106
+
## Zusammenfassung
107
107
108
-
Arrow functions are handy for one-liners. They come in two flavors:
108
+
Pfeilfunktionen sind praktische Einzeiler. Es gibt sie inzwei Varianten:
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.
110
+
1.Ohne geschweifte Klammern:`(...args) => expression`--die rechte Seite ist ein Ausdruck: die Funktion wertet diesen aus und gibt das Ergebnis zurück.
111
+
2.Mit geschweiften Klammern:`(...args) => { body }`--Klammern erlauben es mehrere Anweisungen zu schreiben, aber es braucht ein explizites`return`um etwas zurückzugeben.
0 commit comments