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
Interessant ist, dass es [funktionale](https://de.wikipedia.org/wiki/Funktionale_Programmierung) Programmiersprachen wie [Scala](http://www.scala-lang.org/) oder [Erlang](http://www.erlang.org/) gibt, die das Ändern von Variablenwerten verbieten.
148
+
=======
149
+
````warn header="Declaring twice triggers an error"
150
+
A variable should be declared only once.
151
+
152
+
A repeated declaration of the same variable is an error:
153
+
154
+
```js run
155
+
let message = "This";
156
+
157
+
// repeated 'let' leads to an error
158
+
let message = "That"; // SyntaxError: 'message' has already been declared
159
+
```
160
+
So, we should declare a variable once and then refer to it without `let`.
161
+
````
162
+
163
+
```smart header="Functional languages"
164
+
It's interesting to note that there exist [functional](https://en.wikipedia.org/wiki/Functional_programming) programming languages, like [Scala](http://www.scala-lang.org/) or [Erlang](http://www.erlang.org/) that forbid changing variable values.
165
+
>>>>>>> d35baee32dcce127a69325c274799bb81db1afd8
140
166
141
167
In solchen Sprachen ist der Wert, sobald er "in der Kiste" gespeichert ist, für immer da. Wenn wir etwas anderes speichern wollen, zwingt uns die Sprache dazu, eine neue Kiste zu erstellen (eine neue Variable zu deklarieren). Wir können die alte nicht wiederverwenden.
142
168
@@ -190,7 +216,11 @@ let имя = '...';
190
216
let 我 = '...';
191
217
```
192
218
219
+
<<<<<<< HEAD
193
220
Technisch gesehen gibt es hier keinen Fehler, solche Namen sind erlaubt, aber es gibt eine internationale Tradition, Englisch in Variablennamen zu verwenden. Selbst wenn wir ein kleines Script schreiben, kann es ein langes Leben vor sich haben. Menschen aus anderen Ländern müssen es vielleicht irgendwann einmal lesen.
221
+
=======
222
+
Technically, there is no error here. Such names are allowed, but there is an international convention to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it some time.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/04-var/article.md
+28-17Lines changed: 28 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,27 +13,18 @@ In the very first chapter about [variables](info:variables), we mentioned three
13
13
2.`const`
14
14
3.`var`
15
15
16
-
`let` and `const` behave exactly the same way in terms of Lexical Environments.
17
-
18
-
But `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
19
-
20
-
If you don't plan on meeting such scripts you may even skip this chapter or postpone it, but then there's a chance that it bites you later.
21
-
22
-
From the first sight, `var` behaves similar to `let`. That is, declares a variable:
16
+
The `var` declaration is similar to `let`. Most of the time we can replace `let` by `var` or vice-versa and expect things to work:
23
17
24
18
```js run
25
-
functionsayHi() {
26
-
var phrase ="Hello"; // local variable, "var" instead of "let"
27
-
28
-
alert(phrase); // Hello
29
-
}
19
+
var message ="Hi";
20
+
alert(message); // Hi
21
+
```
30
22
31
-
sayHi();
23
+
But internally `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
32
24
33
-
alert(phrase); // Error, phrase is not defined
34
-
```
25
+
If you don't plan on meeting such scripts you may even skip this chapter or postpone it.
35
26
36
-
...But here are the differences.
27
+
On the other hand, it's important to understand differences when migrating old scripts from `var` to `let`, to avoid odd errors.
37
28
38
29
## "var" has no block scope
39
30
@@ -94,7 +85,27 @@ alert(phrase); // Error: phrase is not defined (Check the Developer Console)
94
85
95
86
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript blocks had no Lexical Environments. And `var` is a remnant of that.
96
87
97
-
## "var" declarations are processed at the function start
88
+
## "var" tolerates redeclarations
89
+
90
+
If we declare the same variable with `let` twice in the same scope, that's an error:
91
+
92
+
```js run
93
+
let user;
94
+
let user; // SyntaxError: 'user' has already been declared
95
+
```
96
+
97
+
With `var`, we can redeclare a variable any number of times. If we use `var` with an already-declared variable, it's just ignored:
98
+
99
+
```js run
100
+
var user ="Pete";
101
+
102
+
var user ="John"; // this "var" does nothing (already declared)
103
+
// ...it doesn't trigger an error
104
+
105
+
alert(user); // John
106
+
```
107
+
108
+
## "var" variables can be declared below their use
98
109
99
110
`var` declarations are processed when the function starts (or script starts for globals).
Copy file name to clipboardExpand all lines: 2-ui/2-events/02-bubbling-and-capturing/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,7 @@ The code sets click handlers on *every* element in the document to see which one
181
181
If you click on `<p>`, then the sequence is:
182
182
183
183
1.`HTML` -> `BODY` -> `FORM` -> `DIV` (capturing phase, the first listener):
184
-
2.`P` (target phrase, triggers two times, as we've set two listeners: capturing and bubbling)
184
+
2.`P` (target phase, triggers two times, as we've set two listeners: capturing and bubbling)
185
185
3.`DIV` -> `FORM` -> `BODY` -> `HTML` (bubbling phase, the second listener).
186
186
187
187
There's a property `event.eventPhase` that tells us the number of the phase on which the event was caught. But it's rarely used, because we usually know it in the handler.
0 commit comments