Skip to content

Constructor, operator "new" #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Yes, it's possible.
Ja, es ist möglich.

If a function returns an object then `new` returns it instead of `this`.
Wenn eine Funktion ein Objekt zurückgibt, dann gibt `new` dieses statt `this` zurück.

So they can, for instance, return the same externally defined object `obj`:
So können sie zum Beispiel dasselbe extern definierte Objekt `obj` zurückgeben:

```js run no-beautify
let obj = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 2

---

# Two functionsone object
# Zwei Funktionenein Objekt

Is it possible to create functions `A` and `B` so that `new A() == new B()`?
Ist es möglich, Funktionen `A` und `B` zu erstellen, sodass `new A() == new B()`?

```js no-beautify
function A() { ... }
Expand All @@ -16,4 +16,4 @@ let b = new B();
alert( a == b ); // true
```

If it is, then provide an example of their code.
Wenn es möglich ist, gib bitte ein Beispiel für deren Code.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function Calculator() {
let calculator = new Calculator();
calculator.read();

alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );
alert( "Summe=" + calculator.sum() );
alert( "Produkt=" + calculator.mul() );
```

Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
importance: 5
# Erstelle einen neuen Taschenrechner

---
Erstelle eine Konstruktorfunktion `Calculator`, die Objekte mit 3 Methoden erstellt:

# Create new Calculator
- `read()` fordert zwei Werte an und speichert diese als Objekteigenschaften mit den Namen `a` und `b` entsprechend.
- `sum()` gibt die Summe dieser Eigenschaften zurück.
- `mul()` gibt das Produkt der Multiplikation dieser Eigenschaften zurück.

Create a constructor function `Calculator` that creates objects with 3 methods:

- `read()` prompts for two values and saves them as object properties with names `a` and `b` respectively.
- `sum()` returns the sum of these properties.
- `mul()` returns the multiplication product of these properties.

For instance:
Zum Beispiel:

```js
let calculator = new Calculator();
calculator.read();

alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );
alert( "Summe=" + calculator.sum() );
alert( "Produkt=" + calculator.mul() );
```

[demo]
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@


```js run demo
function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.value += +prompt('Wieviel soll hinzugefügt werden?', 0);
};

}
Expand All @@ -15,3 +13,4 @@ accumulator.read();
accumulator.read();
alert(accumulator.value);
```

26 changes: 11 additions & 15 deletions 1-js/04-object-basics/06-constructor-new/3-accumulator/task.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
importance: 5
# Neuen Akkumulator erstellen

---
Erstelle eine Konstruktorfunktion `Accumulator(startingValue)`.

# Create new Accumulator
Das Objekt, das es erstellt, sollte:

Create a constructor function `Accumulator(startingValue)`.
- Den "aktuellen Wert" in der Eigenschaft `value` speichern. Der Anfangswert wird auf den Argumentwert des Konstruktors `startingValue` gesetzt.
- Die `read()` Methode sollte `prompt` verwenden, um eine neue Zahl zu lesen und diese zu `value` hinzuzufügen.

Object that it creates should:
Anders ausgedrückt, die Eigenschaft `value` ist die Summe aller von Benutzern eingegebenen Werte mit dem Anfangswert `startingValue`.

- Store the "current value" in the property `value`. The starting value is set to the argument of the constructor `startingValue`.
- The `read()` method should use `prompt` to read a new number and add it to `value`.

In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.

Here's the demo of the code:
Hier ist eine Demo des Codes:

```js
let accumulator = new Accumulator(1); // initial value 1
let accumulator = new Accumulator(1); // Anfangswert 1

accumulator.read(); // adds the user-entered value
accumulator.read(); // adds the user-entered value
accumulator.read(); // addiert den vom Benutzer eingegebenen Wert
accumulator.read(); // addiert den vom Benutzer eingegebenen Wert

alert(accumulator.value); // shows the sum of these values
alert(accumulator.value); // zeigt die Summe dieser Werte
```

[demo]
Loading