Skip to content

Commit 73b931a

Browse files
authored
Merge pull request #122 from otmon76/1.4.5
Constructor, operator "new"
2 parents 53d8de8 + 78bf078 commit 73b931a

File tree

11 files changed

+180
-180
lines changed

11 files changed

+180
-180
lines changed

1-js/04-object-basics/06-constructor-new/1-two-functions-one-object/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Yes, it's possible.
1+
Ano, je to možné.
22

3-
If a function returns an object then `new` returns it instead of `this`.
3+
Jestliže funkce vrací objekt, pak jej `new` vrátí místo `this`.
44

5-
So they can, for instance, return the same externally defined object `obj`:
5+
Mohou tedy například vrátit stejný externě definovaný objekt `obj`:
66

77
```js run no-beautify
88
let obj = {};

1-js/04-object-basics/06-constructor-new/1-two-functions-one-object/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Two functionsone object
5+
# Dvě funkcejeden objekt
66

7-
Is it possible to create functions `A` and `B` so that `new A() == new B()`?
7+
Je možné vytvořit funkce `A` a `B` tak, aby `new A() == new B()`?
88

99
```js no-beautify
1010
function A() { ... }
@@ -16,4 +16,4 @@ let b = new B();
1616
alert( a == b ); // true
1717
```
1818

19-
If it is, then provide an example of their code.
19+
Pokud ano, uveďte příklad jejich kódu.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
function Calculator() {
1+
function Kalkulátor() {
22

3-
this.read = function() {
3+
this.načti = function() {
44
this.a = +prompt('a?', 0);
55
this.b = +prompt('b?', 0);
66
};
77

8-
this.sum = function() {
8+
this.součet = function() {
99
return this.a + this.b;
1010
};
1111

12-
this.mul = function() {
12+
this.součin = function() {
1313
return this.a * this.b;
1414
};
1515
}

1-js/04-object-basics/06-constructor-new/2-calculator-constructor/_js.view/test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11

2-
describe("calculator", function() {
3-
let calculator;
2+
describe("kalkulátor", function() {
3+
let kalkulátor;
44
before(function() {
55
sinon.stub(window, "prompt")
66

77
prompt.onCall(0).returns("2");
88
prompt.onCall(1).returns("3");
99

10-
calculator = new Calculator();
11-
calculator.read();
10+
kalkulátor = new Kalkulátor();
11+
kalkulátor.read();
1212
});
1313

14-
it("the read method asks for two values using prompt and remembers them in object properties", function() {
15-
assert.equal(calculator.a, 2);
16-
assert.equal(calculator.b, 3);
14+
it("funkce načti se zeptá na dvě hodnoty pomocí prompt a zapamatuje si je jako vlastnosti objektu", function() {
15+
assert.equal(kalkulátor.a, 2);
16+
assert.equal(kalkulátor.b, 3);
1717
});
1818

19-
it("when 2 and 3 are entered, the sum is 5", function() {
20-
assert.equal(calculator.sum(), 5);
19+
it("když zadáme 2 a 3, součet je 5", function() {
20+
assert.equal(kalkulátor.součet(), 5);
2121
});
2222

23-
it("when 2 and 3 are entered, the product is 6", function() {
24-
assert.equal(calculator.mul(), 6);
23+
it("když zadáme 2 a 3, součin je 6", function() {
24+
assert.equal(kalkulátor.součin(), 6);
2525
});
2626

2727
after(function() {
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
```js run demo
2-
function Calculator() {
2+
function Kalkulátor() {
33

4-
this.read = function() {
4+
this.načti = function() {
55
this.a = +prompt('a?', 0);
66
this.b = +prompt('b?', 0);
77
};
88

9-
this.sum = function() {
9+
this.součet = function() {
1010
return this.a + this.b;
1111
};
1212

13-
this.mul = function() {
13+
this.součin = function() {
1414
return this.a * this.b;
1515
};
1616
}
1717

18-
let calculator = new Calculator();
19-
calculator.read();
18+
let kalkulátor = new Kalkulátor();
19+
kalkulátor.načti();
2020

21-
alert( "Sum=" + calculator.sum() );
22-
alert( "Mul=" + calculator.mul() );
21+
alert( "Součet=" + kalkulátor.součet() );
22+
alert( "Součin=" + kalkulátor.součin() );
2323
```

1-js/04-object-basics/06-constructor-new/2-calculator-constructor/task.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ importance: 5
22

33
---
44

5-
# Create new Calculator
5+
# Vytvořte nový Kalkulátor
66

7-
Create a constructor function `Calculator` that creates objects with 3 methods:
7+
Vytvořte konstruktor `Kalkulátor`, který bude vytvářet objekty se třemi metodami:
88

9-
- `read()` prompts for two values and saves them as object properties with names `a` and `b` respectively.
10-
- `sum()` returns the sum of these properties.
11-
- `mul()` returns the multiplication product of these properties.
9+
- `načti()` se funkcí `prompt` zeptá na dvě hodnoty a uloží je jako vlastnosti objektu s názvy po řadě `a` a `b`.
10+
- `součet()` vrátí součet těchto hodnot.
11+
- `součin()` vrátí součin těchto hodnot.
1212

13-
For instance:
13+
Například:
1414

1515
```js
16-
let calculator = new Calculator();
17-
calculator.read();
16+
let kalkulátor = new Kalkulátor();
17+
kalkulátor.načti();
1818

19-
alert( "Sum=" + calculator.sum() );
20-
alert( "Mul=" + calculator.mul() );
19+
alert( "Součet=" + kalkulátor.součet() );
20+
alert( "Součin=" + kalkulátor.součin() );
2121
```
2222

2323
[demo]
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
function Accumulator(startingValue) {
2-
this.value = startingValue;
1+
function Akumulátor(počátečníHodnota) {
2+
this.hodnota = počátečníHodnota;
33

4-
this.read = function() {
5-
this.value += +prompt('How much to add?', 0);
4+
this.načti = function() {
5+
this.hodnota += +prompt('Kolik přičíst?', 0);
66
};
77

8-
}
8+
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe("Accumulator", function() {
1+
describe("Akumulátor", function() {
22

33
beforeEach(function() {
44
sinon.stub(window, "prompt")
@@ -8,23 +8,23 @@ describe("Accumulator", function() {
88
prompt.restore();
99
});
1010

11-
it("initial value is the argument of the constructor", function() {
12-
let accumulator = new Accumulator(1);
11+
it("úvodní hodnota je argument konstruktoru", function() {
12+
let akumulátor = new Akumulátor(1);
1313

14-
assert.equal(accumulator.value, 1);
14+
assert.equal(akumulátor.hodnota, 1);
1515
});
1616

17-
it("after reading 0, the value is 1", function() {
18-
let accumulator = new Accumulator(1);
17+
it("po načtení 0 je hodnota 1", function() {
18+
let akumulátor = new Akumulátor(1);
1919
prompt.returns("0");
20-
accumulator.read();
21-
assert.equal(accumulator.value, 1);
20+
akumulátor.načti();
21+
assert.equal(akumulátor.hodnota, 1);
2222
});
2323

24-
it("after reading 1, the value is 2", function() {
25-
let accumulator = new Accumulator(1);
24+
it("po načtení 1 je hodnota 2", function() {
25+
let akumulátor = new Akumulátor(1);
2626
prompt.returns("1");
27-
accumulator.read();
28-
assert.equal(accumulator.value, 2);
27+
akumulátor.načti();
28+
assert.equal(akumulátor.hodnota, 2);
2929
});
3030
});
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

22

33
```js run demo
4-
function Accumulator(startingValue) {
5-
this.value = startingValue;
4+
function Akumulátor(počátečníHodnota) {
5+
this.hodnota = počátečníHodnota;
66

7-
this.read = function() {
8-
this.value += +prompt('How much to add?', 0);
7+
this.načti = function() {
8+
this.hodnota += +prompt('Kolik přičíst?', 0);
99
};
1010

1111
}
1212

13-
let accumulator = new Accumulator(1);
14-
accumulator.read();
15-
accumulator.read();
16-
alert(accumulator.value);
13+
let akumulátor = new Akumulátor(1);
14+
akumulátor.načti();
15+
akumulátor.načti();
16+
alert(akumulátor.hodnota);
1717
```

1-js/04-object-basics/06-constructor-new/3-accumulator/task.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ importance: 5
22

33
---
44

5-
# Create new Accumulator
5+
# Vytvořte nový Akumulátor
66

7-
Create a constructor function `Accumulator(startingValue)`.
7+
Vytvořte konstruktor `Akumulátor(počátečníHodnota)`.
88

9-
Object that it creates should:
9+
Objekt, který je tímto konstruktorem vytvořen, by měl:
1010

11-
- Store the "current value" in the property `value`. The starting value is set to the argument of the constructor `startingValue`.
12-
- The `read()` method should use `prompt` to read a new number and add it to `value`.
11+
- Ukládat „aktuální hodnotu“ do vlastnosti `hodnota`. Počáteční hodnota se nastaví na argument konstruktoru `počátečníHodnota`.
12+
- Metoda `načti()` by se měla pomocí `prompt` zeptat na nové číslo a přičíst je k vlastnosti `hodnota`.
1313

14-
In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
14+
Jinými slovy, vlastnost `hodnota` bude součet všech uživatelem zadaných hodnot a úvodní hodnoty `počátečníHodnota`.
1515

16-
Here's the demo of the code:
16+
Zde je ukázka kódu:
1717

1818
```js
19-
let accumulator = new Accumulator(1); // initial value 1
19+
let akumulátor = new Akumulátor(1); // počáteční hodnota 1
2020

21-
accumulator.read(); // adds the user-entered value
22-
accumulator.read(); // adds the user-entered value
21+
akumulátor.načti(); // přičte uživatelem zadanou hodnotu
22+
akumulátor.načti(); // přičte uživatelem zadanou hodnotu
2323

24-
alert(accumulator.value); // shows the sum of these values
24+
alert(akumulátor.hodnota); // zobrazí součet těchto hodnot
2525
```
2626

2727
[demo]

0 commit comments

Comments
 (0)