en.javascript.info/1-js/8-oop/5-functional-inheritance/3-inherit-fridge/solution.md
Ilya Kantor 87bf53d076 update
2014-11-16 01:40:20 +03:00

29 lines
793 B
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.

Решение:
```js
function Fridge(power) {
// унаследовать
Machine.apply(this, arguments);
var food = []; // приватное свойство food
this.addFood = function() {
if (!this._enabled) {
throw new Error("Холодильник выключен");
}
if (food.length + arguments.length >= this._power / 100) {
throw new Error("Нельзя добавить, не хватает мощности");
}
for(var i=0; i<arguments.length; i++) {
food.push(arguments[i]); // добавить всё из arguments
}
};
this.getFood = function() {
// копируем еду в новый массив, чтобы манипуляции с ним не меняли food
return food.slice();
};
}
```