Skip to content

Function expressions#29

Open
BitcoinElf wants to merge 6 commits intojavascript-tutorial:masterfrom
BitcoinElf:patch-1
Open

Function expressions#29
BitcoinElf wants to merge 6 commits intojavascript-tutorial:masterfrom
BitcoinElf:patch-1

Conversation

@BitcoinElf
Copy link

German Translation of article Function Expressions

@javascript-translate-bot javascript-translate-bot added the review needed Review needed, please approve or request changes label Mar 31, 2020
@javascript-translate-bot javascript-translate-bot requested a review from a team March 31, 2020 11:36
@CLAassistant
Copy link

CLAassistant commented Mar 31, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@christianhegedues christianhegedues left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @BitcoinElf vielen Dank für deinen Pull-Request.
Ich habe nur bis zur Zeile 43 eine Review durchgeführt, da irgendwo ab Zeile 40 die Formatierung des Originals verloren geht. Kannst du das bitte wieder anpassen, danach werden ich mir den Rest ansehen.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Bisher haben wir die Syntax *Function Declaration* benutzt.
Die Syntax, die wir zuvor verwendet haben, wird *Funktionsdeklaration* genannt:

```

There is another syntax for creating a function that is called a *Function Expression*.
Es gibt allerdings auch die Syntax namens *Function Expression*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Es gibt allerdings auch die Syntax namens *Function Expression*
Es gibt eine weitere Syntax zum Erstellen einer Funktion, die als *Funktionsausdruck* bezeichnet wird.


```js
let sayHi = function() {
alert( "Hello" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
alert( "Hello" );
alert( "Hallo" );

```

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

```

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

```js run no-beautify
function sayHi() { // (1) create
function sayHi() { // (1) erstellen
alert( "Hello" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
alert( "Hello" );
alert( "Hallo" );


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)!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func(); // Hello // (3) Kopie ausführen(klappt)!
func(); // Hallo // (3) führe die Kopie aus (es funktioniert)!

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?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sayHi(); // Hello // Die Funktion funktioniert immernoch (warum auch nicht?)
sayHi(); // Hallo // das funktioniert immer noch (warum sollte es nicht)

@javascript-translate-bot javascript-translate-bot added changes requested Waiting for changes and /done from PR author and removed review needed Review needed, please approve or request changes labels Apr 5, 2020
@javascript-translate-bot

Please make the requested changes. After it, add a comment "/done".
Then I'll ask for a new review 👻

Co-authored-by: Christian Hegedüs <christianhegedues@users.noreply.github.com>
Copy link
Contributor

@engag1ng engag1ng left a comment

Choose a reason for 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

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsausdruck"

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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsdeklaration"

````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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsausdrücke"

- 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Ein Funktionsausdruck"

## 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionen", minor typo

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsausdruck"

- 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsdeklarationen"

- 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsausdrücke"

- 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsdeklaration"

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Funktionsausdruck"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes requested Waiting for changes and /done from PR author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants