|
1 | | -# Code structure |
| 1 | +# Code-Struktur |
2 | 2 |
|
3 | | -The first thing we'll study is the building blocks of code. |
| 3 | +Das Erste, was wir untersuchen werden, sind die Bausteine des Codes. |
4 | 4 |
|
5 | | -## Statements |
| 5 | +## Anweisungen |
6 | 6 |
|
7 | | -Statements are syntax constructs and commands that perform actions. |
| 7 | +Anweisungen sind Syntaxkonstrukte und Befehle, die Aktionen ausführen. |
8 | 8 |
|
9 | | -We've already seen a statement, `alert('Hello, world!')`, which shows the message "Hello, world!". |
| 9 | +Wir haben bereits eine Anweisung `alert('Hallo, Welt!')` gesehen, welche die Meldung "Hallo, Welt!" zeigt. |
10 | 10 |
|
11 | | -We can have as many statements in our code as we want. Statements can be separated with a semicolon. |
| 11 | +Wir können so viele Anweisungen in unserem Code haben, wie wir wollen. Anweisungen können durch ein Semikolon getrennt werden. |
12 | 12 |
|
13 | | -For example, here we split "Hello World" into two alerts: |
| 13 | +Zum Beispiel teilen wir hier "Hallo Welt" in zwei Warnungen: |
14 | 14 |
|
15 | 15 | ```js run no-beautify |
16 | | -alert('Hello'); alert('World'); |
| 16 | +alert('Hallo'); alert('Welt'); |
17 | 17 | ``` |
18 | 18 |
|
19 | | -Usually, statements are written on separate lines to make the code more readable: |
| 19 | +Normalerweise werden Anweisungen in separate Zeilen geschrieben, um den Code besser lesbar zu machen: |
20 | 20 |
|
21 | 21 | ```js run no-beautify |
22 | | -alert('Hello'); |
23 | | -alert('World'); |
| 22 | +alert('Hallo'); |
| 23 | +alert('Welt'); |
24 | 24 | ``` |
25 | 25 |
|
26 | | -## Semicolons [#semicolon] |
| 26 | +## Semikolons [#semicolon] |
27 | 27 |
|
28 | | -A semicolon may be omitted in most cases when a line break exists. |
| 28 | +Ein Semikolon kann in den meisten Fällen weggelassen werden, wenn ein Zeilenumbruch vorliegt. |
29 | 29 |
|
30 | | -This would also work: |
| 30 | +Das würde auch funktionieren: |
31 | 31 |
|
32 | 32 | ```js run no-beautify |
33 | | -alert('Hello') |
34 | | -alert('World') |
| 33 | +alert('Hallo') |
| 34 | +alert('Welt') |
35 | 35 | ``` |
36 | 36 |
|
37 | | -Here, JavaScript interprets the line break as an "implicit" semicolon. This is called an [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion). |
| 37 | +Hier interpretiert JavaScript den Zeilenumbruch als "implizites" Semikolon. Dies wird als [automatische Semikoloneinfügung](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion) bezeichnet. |
38 | 38 |
|
39 | | -**In most cases, a newline implies a semicolon. But "in most cases" does not mean "always"!** |
| 39 | +**In den meisten Fällen bedeutet ein Zeilenumbruch ein Semikolon. "Meistens" heißt aber nicht "immer"!** |
40 | 40 |
|
41 | | -There are cases when a newline does not mean a semicolon. For example: |
| 41 | +Es gibt Fälle, in denen ein Zeilenumbruch kein Semikolon bedeutet. Beispielsweise: |
42 | 42 |
|
43 | 43 | ```js run no-beautify |
44 | 44 | alert(3 + |
45 | 45 | 1 |
46 | 46 | + 2); |
47 | 47 | ``` |
48 | 48 |
|
49 | | -The code outputs `6` because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression", so the semicolon is not required. And in this case that works as intended. |
| 49 | +Der Code gibt `6` aus, da JavaScript hier keine Semikolons einfügt. Es ist intuitiv klar, dass, wenn die Zeile mit einem Pluszeichen `"+"` endet, es sich um einen "unvollständigen Ausdruck" handelt, sodass das Semikolon nicht erforderlich ist. Und in diesem Fall funktioniert das wie vorgesehen. |
50 | 50 |
|
51 | | -**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.** |
| 51 | +**Es gibt jedoch Situationen, in denen JavaScript ein Semikolon nicht annimmt, wenn es wirklich benötigt wird.** |
52 | 52 |
|
53 | | -Errors which occur in such cases are quite hard to find and fix. |
| 53 | +Fehler, die in solchen Fällen auftreten, sind schwer zu finden und zu beheben. |
54 | 54 |
|
55 | | -````smart header="An example of an error" |
56 | | -If you're curious to see a concrete example of such an error, check this code out: |
| 55 | +````smart header="Ein Beispiel für einen Fehler" |
| 56 | +Wenn du ein konkretes Beispiel für einen solchen Fehler sehen möchten, lies den folgenden Code: |
57 | 57 |
|
58 | 58 | ```js run |
59 | 59 | [1, 2].forEach(alert) |
60 | 60 | ``` |
61 | 61 |
|
62 | | -No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`. |
| 62 | +Über die Bedeutung der Klammern `[]` und `forEach` muss noch nicht nachgedacht werden. Wir werden sie später studieren. Denk vorerst nur an das Ergebnis des Codes: Es zeigt `1`, dann `2`. |
63 | 63 |
|
64 | | -Now, let's add an `alert` before the code and *not* finish it with a semicolon: |
| 64 | +Fügen wir nun vor dem Code einen `alert` ein und beenden ihn *nicht* mit einem Semikolon: |
65 | 65 |
|
66 | 66 | ```js run no-beautify |
67 | | -alert("There will be an error") |
| 67 | +alert("Es wird ein Fehler auftreten") |
68 | 68 |
|
69 | 69 | [1, 2].forEach(alert) |
70 | 70 | ``` |
71 | 71 |
|
72 | | -Now if we run the code, only the first `alert` is shown and then we have an error! |
| 72 | +Wenn wir nun den Code ausführen, wird nur der erste `alert` angezeigt und dann haben wir einen Fehler! |
73 | 73 |
|
74 | | -But everything is fine again if we add a semicolon after `alert`: |
| 74 | +Aber alles ist wieder in Ordnung, wenn wir nach `alert` ein Semikolon einfügen: |
75 | 75 | ```js run |
76 | | -alert("All fine now"); |
| 76 | +alert("Jetzt ist alles in Ordnung"); |
77 | 77 |
|
78 | | -[1, 2].forEach(alert) |
| 78 | +[1, 2].forEach(alert) |
79 | 79 | ``` |
80 | 80 |
|
81 | | -Now we have the "All fine now" message followed by `1` and `2`. |
| 81 | +Jetzt haben wir die Nachricht "Jetzt ist alles in Ordnung" gefolgt von `1` und `2`. |
82 | 82 |
|
83 | 83 |
|
84 | | -The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets `[...]`. |
| 84 | +Der Fehler in der Variante ohne Semikolon tritt auf, weil JavaScript kein Semikolon vor eckigen Klammern `[...]` annimmt. |
85 | 85 |
|
86 | | -So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here's how the engine sees it: |
| 86 | +Da das Semikolon nicht automatisch eingefügt wird, wird der Code im ersten Beispiel als einzelne Anweisung behandelt. So sieht es die Engine: |
87 | 87 |
|
88 | 88 | ```js run no-beautify |
89 | | -alert("There will be an error")[1, 2].forEach(alert) |
| 89 | +alert("Es wird ein Fehler auftreten")[1, 2].forEach(alert) |
90 | 90 | ``` |
91 | 91 |
|
92 | | -But it should be two separate statements, not one. Such a merging in this case is just wrong, hence the error. This can happen in other situations. |
| 92 | +Aber es sollten zwei getrennte Anweisungen sein, nicht eine. Eine solche Verschmelzung ist in diesem Fall einfach falsch, daher der Fehler. Dies kann auch in anderen Situationen auftreten. |
93 | 93 | ```` |
94 | 94 |
|
95 | | -We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of the time. But it's safer -- especially for a beginner -- to use them. |
| 95 | +Es wird empfohlen, Semikolons zwischen Anweisungen zu setzen, auch wenn diese durch Zeilenumbrüche getrennt sind. Diese Regel wird von der Community weitgehend übernommen. Lass uns noch einmal festhalten -- es ist möglich, Semikolons die meiste Zeit wegzulassen. Aber es ist sicherer -- besonders für Anfänger -- sie zu benutzen. |
96 | 96 |
|
97 | | -## Comments |
| 97 | +## Kommentare [#code-comments] |
98 | 98 |
|
99 | | -As time goes on, programs become more and more complex. It becomes necessary to add *comments* which describe what the code does and why. |
| 99 | +Mit der Zeit werden Programme immer komplexer. Es müssen *Kommentare* hinzugefügt werden, die beschreiben, was der Code macht und warum. |
100 | 100 |
|
101 | | -Comments can be put into any place of a script. They don't affect its execution because the engine simply ignores them. |
| 101 | +Kommentare können an jeder Stelle eines Skripts eingefügt werden. Sie haben keinen Einfluss auf die Ausführung, da die Engine sie einfach ignoriert. |
102 | 102 |
|
103 | | -**One-line comments start with two forward slash characters `//`.** |
| 103 | +**Einzeilige Kommentare beginnen mit zwei Schrägstrichen `//`.** |
104 | 104 |
|
105 | | -The rest of the line is a comment. It may occupy a full line of its own or follow a statement. |
| 105 | +Der Rest der Zeile ist ein Kommentar. Er kann eine ganze eigene Zeile belegen oder einer Anweisung folgen. |
106 | 106 |
|
107 | | -Like here: |
| 107 | +Wie hier: |
108 | 108 | ```js run |
109 | | -// This comment occupies a line of its own |
110 | | -alert('Hello'); |
| 109 | +// Dieser Kommentar belegt eine eigene Zeile |
| 110 | +alert('Hallo'); |
111 | 111 |
|
112 | | -alert('World'); // This comment follows the statement |
| 112 | +alert('Welt'); // Dieser Kommentar folgt der Anweisung |
113 | 113 | ``` |
114 | 114 |
|
115 | | -**Multiline comments start with a forward slash and an asterisk <code>/*</code> and end with an asterisk and a forward slash <code>*/</code>.** |
| 115 | +**Mehrzeilige Kommentare beginnen mit einem Schrägstrich und einem Sternchen <code>/*</code> und enden mit einem Sternchen und einem Schrägstrich <code>*/</code>.** |
116 | 116 |
|
117 | | -Like this: |
| 117 | +So wie hier: |
118 | 118 |
|
119 | 119 | ```js run |
120 | | -/* An example with two messages. |
121 | | -This is a multiline comment. |
| 120 | +/* Ein Beispiel mit zwei Nachrichten. |
| 121 | +Dies ist ein mehrzeiliger Kommentar. |
122 | 122 | */ |
123 | | -alert('Hello'); |
124 | | -alert('World'); |
| 123 | +alert('Hallo'); |
| 124 | +alert('Welt'); |
125 | 125 | ``` |
126 | 126 |
|
127 | | -The content of comments is ignored, so if we put code inside <code>/* ... */</code>, it won't execute. |
| 127 | +Der Inhalt von Kommentaren wird ignoriert. Wenn wir also Code innerhalb <code>/* ... */</code> einfügen, wird er nicht ausgeführt. |
128 | 128 |
|
129 | | -Sometimes it can be handy to temporarily disable a part of code: |
| 129 | +Manchmal kann es nützlich sein, einen Teil des Codes vorübergehend zu deaktivieren: |
130 | 130 |
|
131 | 131 | ```js run |
132 | | -/* Commenting out the code |
133 | | -alert('Hello'); |
| 132 | +/* Code auskommentieren |
| 133 | +alert('Hallo'); |
134 | 134 | */ |
135 | | -alert('World'); |
| 135 | +alert('Welt'); |
136 | 136 | ``` |
137 | 137 |
|
| 138 | +<<<<<<< HEAD |
| 139 | +```smart header="Benutze Tastenkombination!" |
| 140 | +In den meisten Editoren kannst du eine Codezeile auskommentieren, indem du die Tastenkombination `key:Strg+/` für einen einzeiligen Kommentar und die Tastenkombination `key:Strg+Umschalt+/` für mehrzeilige Kommentare drückst (wähle ein Stück Code aus und drücke die Tastenkombination). Für Mac versuche "key:Cmd" anstelle von "key:Strg" und `key:Option` statt `key:Umschalt`. |
| 141 | +======= |
138 | 142 | ```smart header="Use hotkeys!" |
139 | | -In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl`. |
| 143 | +In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl` and `key:Option` instead of `key:Shift`. |
| 144 | +>>>>>>> 23ffde780605b3418101850a61d9496c3d7f927c |
140 | 145 | ``` |
141 | 146 |
|
142 | | -````warn header="Nested comments are not supported!" |
143 | | -There may not be `/*...*/` inside another `/*...*/`. |
| 147 | +````warn header="Verschachtelte Kommentare werden nicht unterstützt!" |
| 148 | +Es ist nicht möglich `/*...*/` innerhalb eines anderen `/*...*/` zu setzen. |
144 | 149 |
|
145 | | -Such code will die with an error: |
| 150 | +Solcher Code resultiert in einem Fehler: |
146 | 151 |
|
147 | 152 | ```js run no-beautify |
148 | 153 | /* |
149 | | - /* nested comment ?!? */ |
| 154 | + /* verschachtelter Kommentar ?!? */ |
150 | 155 | */ |
151 | | -alert( 'World' ); |
| 156 | +alert( 'Welt' ); |
152 | 157 | ``` |
153 | 158 | ```` |
154 | 159 |
|
155 | | -Please, don't hesitate to comment your code. |
| 160 | +Bitte zögere nicht, deinen Code zu kommentieren. |
156 | 161 |
|
157 | | -Comments increase the overall code footprint, but that's not a problem at all. There are many tools which minify code before publishing to a production server. They remove comments, so they don't appear in the working scripts. Therefore, comments do not have negative effects on production at all. |
| 162 | +Kommentare vergrößern den gesamten Code-Fussabdruck, aber das ist überhaupt kein Problem. Es gibt viele Werkzeuge, die den Code vor dem Veröffentlichen auf einem Produktionsserver minimieren. Sie entfernen Kommentare, sodass sie nicht in den Arbeitsskripten angezeigt werden. Kommentare wirken sich daher überhaupt nicht negativ auf die Produktion aus. |
158 | 163 |
|
159 | | -Later in the tutorial there will be a chapter <info:code-quality> that also explains how to write better comments. |
| 164 | +Später im Tutorial wird es ein Kapitel <info:code-quality> geben, in dem auch erklärt wird, wie man bessere Kommentare schreibt. |
0 commit comments