Skip to content
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

1-js/04-object-basics/06-constructor-new #127

Merged
merged 27 commits into from
May 10, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Yes, it's possible.
Да, возможно.

If a function returns an object then `new` returns it instead of `this`.
Если функция возвращает объект, то вместо `this` будет возвращен этот объект.

So they can, for instance, return the same externally defined object `obj`:
Например, они могут вернуть один и тот же объект `obj`, определённый снаружи:

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

---

# Two functions – one object
# Две функции - один объект

Возможно ли создать функции `A` и `B` в примере ниже, где объекты равны `new A()==new B()`?

Is it possible to create functions `A` and `B` such as `new A()==new B()`?

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

If it is, then provide an example of their code.
Если да - приведите пример вашего кода.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ importance: 5

---

# Create new Calculator
# Создание калькулятора при помощи конструктора

Create a constructor function `Calculator` that creates objects with 3 methods:
Создайте функцию-конструктор `Calculator`, который создаёт объекты с тремя методами:

- `read()` asks for two values using `prompt` and remembers them in object properties.
- `sum()` returns the sum of these properties.
- `mul()` returns the multiplication product of these properties.
- `read()` запрашивает два значения при помощи `prompt` и сохраняет их значение в свойствах объекта.
- `sum()` возвращает сумму введенных свойств.
- `mul()` возвращает произведение введённых свойств.

For instance:

Например:

```js
let calculator = new Calculator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.value += +prompt('Сколько нужно добавить?', 0);
};

}
Expand Down
22 changes: 11 additions & 11 deletions 1-js/04-object-basics/06-constructor-new/3-accumulator/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ importance: 5

---

# Create new Accumulator
# Создаем Accumulator

Create a constructor function `Accumulator(startingValue)`.
Создайте функцию-конструктор `Accumulator(startingValue)`.

Object that it creates should:
Созданный объект должен уметь следующее:

- 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`.
- Хранить "текущее значение" в свойстве `value`. Начальное значение устанавливается в аргументе конструктора `startingValue`.
- Метод `read()` использует `prompt` для получения числа и прибавляет его к свойству `value`.

In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
Таким образом, свойство `value` является текущей суммой всего, что ввёл пользователь при вызовах метода `read()`, с учётом начального значения `startingValue`.

Here's the demo of the code:
Ниже вы можете посмотреть работу кода:

```js
let accumulator = new Accumulator(1); // initial value 1
accumulator.read(); // adds the user-entered value
accumulator.read(); // adds the user-entered value
alert(accumulator.value); // shows the sum of these values
let accumulator = new Accumulator(1); // начальное значение 1
accumulator.read(); // прибавит ввод prompt к текущему значению
accumulator.read(); // прибавит ввод prompt к текущему значению
alert(accumulator.value); // выведет текущее значение
```

[demo]