GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Function expressions by BitcoinElf · Pull Request #29 · javascript-tutorial/de.javascript.info Conversation
|
There was an error while loading. Please reload this page.
| In JavaScript ist eine Funktion keine "magische Sprachstruktur", sondern eine besondere Art Wert. | ||
|
|
||
| The syntax that we used before is called a *Function Declaration*: | ||
| Bisher haben wir die Syntax *Function Declaration* benutzt. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| Bisher haben wir die Syntax *Function Declaration* benutzt. | |
| Die Syntax, die wir zuvor verwendet haben, wird *Funktionsdeklaration* genannt: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ``` | ||
|
|
||
| There is another syntax for creating a function that is called a *Function Expression*. | ||
| Es gibt allerdings auch die Syntax namens *Function Expression* |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| Es gibt allerdings auch die Syntax namens *Function Expression* | |
| Es gibt eine weitere Syntax zum Erstellen einer Funktion, die als *Funktionsausdruck* bezeichnet wird. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| ```js | ||
| let sayHi = function() { | ||
| alert( "Hello" ); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| alert( "Hello" ); | |
| alert( "Hallo" ); |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ``` | ||
|
|
||
| Here, the function is created and assigned to the variable explicitly, like any other value. No matter how the function is defined, it's just a value stored in the variable `sayHi`. | ||
| Hier wird die Funktion erstellt und einer Variable explizit zugewiesen, wie jeder andere Wert. Unabhängig davon, wie die Funktion definiert ist, ist sie nur ein Wert, den wir in der variable `sayHi` speichern. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| Hier wird die Funktion erstellt und einer Variable explizit zugewiesen, wie jeder andere Wert. Unabhängig davon, wie die Funktion definiert ist, ist sie nur ein Wert, den wir in der variable `sayHi` speichern. | |
| Hier wird die Funktion erstellt und einer Variable explizit zugewiesen, wie jeder andere Wert. Unabhängig davon, wie die Funktion definiert ist, ist sie nur ein Wert, den wir in der Variable `sayHi` speichern. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ``` | ||
|
|
||
| Please note that the last line does not run the function, because there are no parentheses after `sayHi`. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that. | ||
| Interessanterweise führt die letzte Zeile die Funktion nicht aus, denn hinter `sayHi` befinden sich keine Klammern. In einigen Programmiersprachen führt jede Erwähnung einer Funktion zur Ausführung, JavaScript gehört nicht dazu. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| Interessanterweise führt die letzte Zeile die Funktion nicht aus, denn hinter `sayHi` befinden sich keine Klammern. In einigen Programmiersprachen führt jede Erwähnung einer Funktion zur Ausführung, JavaScript gehört nicht dazu. | |
| Bitte beachte, dass die letzte Zeile die Funktion nicht ausführt, da nach `sayHi` keine Klammern stehen. In einigen Programmiersprachen führt jede Erwähnung einer Funktion zur Ausführung, JavaScript gehört nicht dazu. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Interessanterweise führt die letzte Zeile die Funktion nicht aus, denn hinter `sayHi` befinden sich keine Klammern. In einigen Programmiersprachen führt jede Erwähnung einer Funktion zur Ausführung, JavaScript gehört nicht dazu. | ||
|
|
||
| In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code. | ||
| In JavaScript ist eine Funktion ein Wert, also können wir damit auch umgehen, wie mit einem Wert. Der obige code zeigt die Funktion als String an, zeigt also den Quellcode. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| In JavaScript ist eine Funktion ein Wert, also können wir damit auch umgehen, wie mit einem Wert. Der obige code zeigt die Funktion als String an, zeigt also den Quellcode. | |
| In JavaScript ist eine Funktion ein Wert, so dass wir sie als Wert behandeln können. Der obige Code zeigt seine Zeichenkettendarstellung, die den Quellcode darstellt. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ```js run no-beautify | ||
| function sayHi() { // (1) create | ||
| function sayHi() { // (1) erstellen | ||
| alert( "Hello" ); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| alert( "Hello" ); | |
| alert( "Hallo" ); |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| func(); // Hello // (3) run the copy (it works)! | ||
| sayHi(); // Hello // this still works too (why wouldn't it) | ||
| func(); // Hello // (3) Kopie ausführen(klappt)! |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| func(); // Hello // (3) Kopie ausführen(klappt)! | |
| func(); // Hallo // (3) führe die Kopie aus (es funktioniert)! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| func(); // Hello // (3) run the copy (it works)! | ||
| sayHi(); // Hello // this still works too (why wouldn't it) | ||
| func(); // Hello // (3) Kopie ausführen(klappt)! | ||
| sayHi(); // Hello // Die Funktion funktioniert immernoch (warum auch nicht?) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| sayHi(); // Hello // Die Funktion funktioniert immernoch (warum auch nicht?) | |
| sayHi(); // Hallo // das funktioniert immer noch (warum sollte es nicht) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Please make the requested changes. After it, add a comment "/done". |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Christian Hegedüs <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Overall really good!
Please fix comments for consistency and complete translation
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| 2. Zeile `(2)` kopiert sie in die Variable `func`. Wieder verwenden wir *keine* Klammern nach `sayHi`. Wenn wir Klammern verwenden würden, dann würde `func = sayHi()` das *Ergebnis des Aufrufs* `sayHi()` in Variable `func`kopieren, nicht den Quellcode selbst. | ||
| 3. Jetzt kann die Funktion unter beiden Namen `sayHi()` und `func()` aufgerufen werden. | ||
|
|
||
| Wir hätten in der ersten Zeile auch die Syntax Function Expression nutzen können um `sayHi` zu definieren: |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsausdruck"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Das hier passiert oben im Detail: | ||
|
|
||
| Note that we could also have used a Function Expression to declare `sayHi`, in the first line: | ||
| 1. Die Function Declaration `(1)` definiert die Funktion und schreibt sie in die Variable `sayHi`. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsdeklaration"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ````smart header="Why is there a semicolon at the end?" | ||
| You might wonder, why does Function Expression have a semicolon `;` at the end, but Function Declaration does not: | ||
| ````smart header="Wieso ist da ein Semikolon am Ende?" | ||
| Man könnte sich fragen, wieso Function Expressions einen Semicolon `;` am Ende benötigen und Function Declarations nicht. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsausdrücke"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| - A Function Expression is used inside the statement: `let sayHi = ...;`, as a value. It's not a code block, but rather an assignment. The semicolon `;` is recommended at the end of statements, no matter what the value is. So the semicolon here is not related to the Function Expression itself, it just terminates the statement. | ||
| Die Antwort ist: | ||
| - Man benötigt keinen semicolon `;` am Ende von Blöcken und syntaktischen Strukturen, die `if { ... }`, `for { }`, `function f { }` etc. verwenden. | ||
| - Eine Function Expression dagegen wird im Befehl `let sayHi = ...;` als Wert verwandt. Es ist kein Block Code, sondern eine Zuweisung. Der Semicolon `;` ist am Ende von Statements empfohlen, egal was der Wert ist. Der Semicolon `;` hat an dieser Stelle also nichts mit der Funktion zu tun, sondern beendet nur das Statement. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Ein Funktionsausdruck"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ## Callback Funktionen | ||
|
|
||
| Let's look at more examples of passing functions as values and using function expressions. | ||
| Schauen wir uns noch einige Beispiele an, in denen funktionen als Werte weitergegeben und in Funktionsausdrücken genutzt werden. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionen", minor typo
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Dies hilft auch der Lesbarkeit, denn es ist leichter, nach `function f(…) {…}` im Code zu suchen, als nach `let f = function(…) {…};`. function declarations "springen ins Auge". | ||
|
|
||
| ...But if a Function Declaration does not suit us for some reason, or we need a conditional declaration (we've just seen an example), then Function Expression should be used. | ||
| Aber manchmal benötigen wir eine bedingte Definition (wie eben im Beispiel) oder haben andere Gründe eine Function Expression zu bevorzugen. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsausdruck"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| - Funktionen sind Werte. Sie können überall im Code zugewiesen, kopiert oder deklariert werden. | ||
| - Wenn die Funktion in einem extra Statement im Hauptcode deklariert wird, dann nennt man das "Function Declaration". | ||
| - Wenn sie als Teil eines Ausdrucks definiert wird, nennen wir das "function expression". | ||
| - Function Declarations werden verarbeitet bevor der Block ausgeführt wird. Sie sind im ganzen Block sichtbar. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsdeklarationen"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| - Wenn die Funktion in einem extra Statement im Hauptcode deklariert wird, dann nennt man das "Function Declaration". | ||
| - Wenn sie als Teil eines Ausdrucks definiert wird, nennen wir das "function expression". | ||
| - Function Declarations werden verarbeitet bevor der Block ausgeführt wird. Sie sind im ganzen Block sichtbar. | ||
| - Function Expressions werden erst erstellt, wenn der Ausführungsfluss sie erreicht. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsausdrücke"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| - Function Expressions werden erst erstellt, wenn der Ausführungsfluss sie erreicht. | ||
|
|
||
| In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable. | ||
| Meistens, wenn wir eine Funktion definieren, sollten wir eine Function Declaration nutzen, da sie überall sichtbar ist. Das sorgt für flexiblere Codeorganisation und bessere Lesbarkeit. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsdeklaration"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| Meistens, wenn wir eine Funktion definieren, sollten wir eine Function Declaration nutzen, da sie überall sichtbar ist. Das sorgt für flexiblere Codeorganisation und bessere Lesbarkeit. | ||
|
|
||
| So we should use a Function Expression only when a Function Declaration is not fit for the task. We've seen a couple of examples of that in this chapter, and will see more in the future. | ||
| Daher sollten wir einen Function Expression nur verwenden, wenn die Function Declaration ungeeignet ist. Dazu haben wir einige Beispiele behandelt und werden in Zukunft noch mehr kennenlernen. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
"Funktionsausdruck"
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.