en.javascript.info/1-js/8-oop/3-getters-setters/2-getter-power/task.md
Ilya Kantor 87bf53d076 update
2014-11-16 01:40:20 +03:00

32 lines
No EOL
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Добавить геттер для power
[importance 5]
Добавьте кофеварке геттер для приватного свойства `power`, чтобы внешний код мог узнать мощность кофеварки.
Исходный код:
```js
function CoffeeMachine(power, capacity) {
//...
this.setWaterAmount = function(amount) {
if (amount < 0) {
throw new Error("Значение должно быть положительным");
}
if (amount > capacity) {
throw new Error("Нельзя залить воды больше, чем " + capacity);
}
waterAmount = amount;
};
this.getWaterAmount = function() {
return waterAmount;
};
}
```
Обратим внимание, что ситуация, когда у свойства `power` есть геттер, но нет сеттера -- вполне обычна.
Здесь это означает, что мощность `power` можно указать лишь при создании кофеварки и в дальнейшем её можно прочитать, но нельзя изменить.