Created
April 2, 2017 19:19
-
-
Save severuykhin/4deec33f482376c3d2b6c503a4934173 to your computer and use it in GitHub Desktop.
JavaScript OOP example 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Prototype inheritance | |
//=================================================================== | |
function Machine (power) { | |
var self = this; | |
self._enabled = false; | |
this._power = power; | |
this.enable = function(){ | |
self._enabled = true; | |
console.log(self._enabled); | |
}; | |
this.disable = function () { | |
self._enabled = false; | |
}; | |
}; | |
function CoffieMachine (power) { | |
var waterAmount = 0; | |
this.setWaterAmount = function(amount){ | |
waterAmount = amount; | |
}; | |
this.checkStatus = function(){ | |
return this._enabled; | |
}; | |
this.checkPower = function(){ | |
return this._power; | |
}; | |
this.run = function(){ | |
if (this._enabled === false) { | |
throw new Error('Кофемашина не включена'); | |
return; | |
} else if (waterAmount <= 0) { | |
throw new Error ('Залейте воду'); | |
} else { | |
console.log(`Кофемашина включена. Количество воды: ${waterAmount}`); | |
} | |
}; | |
//Inheritance from Machine | |
Machine.apply(this, arguments); | |
}; | |
var coffeeMachine = new CoffieMachine(10000); | |
//coffeeMachine.enable(); | |
//coffeeMachine.setWaterAmount(1000); | |
//coffeeMachine.run(); | |
function Fridge (power, temp) { | |
Machine.apply(this, arguments); | |
let temperature = temp; | |
let food = []; | |
this.getFoodItem = function (item) { | |
var foodItem = item.toLowerCase(); | |
if (food.indexOf(foodItem) === -1) { throw new Error ('Такой еды нет. Есть: ' + food.slice().join(', ')) } | |
else { | |
return `${foodItem} лежит на полке № ${food.indexOf(foodItem)}`; | |
} | |
}; | |
this.getFood = function(){ | |
return food.slice(); | |
}; | |
this.getTemp = function(){ | |
return temperature; | |
}; | |
this.getPower = function(){ | |
return this._power; | |
}; | |
this.run = function(){ | |
console.log('Running. Working temperature is:' + temperature); | |
}; | |
//Переопредедение метода с сохранением функционала родительского | |
var parentEnable = this.enable; | |
this.enable = function(){ | |
parentEnable.apply(this.arguments); | |
this.run(); | |
}; | |
this.checkStatus = function(){ | |
return this._enabled; | |
}; | |
this.addFood = function(){ | |
if (!this._enabled) { | |
console.log(this._enabled); | |
throw new Error ('Сначала включите холодильник'); | |
} | |
if (arguments.length <=0 ) { | |
throw new Error('Положите еду'); | |
return; | |
} | |
else { | |
for(var i = 0; i < arguments.length; i++){ | |
if (typeof arguments[i] != 'string') { continue; } | |
else { food.push(arguments[i].toLowerCase()); }; | |
} | |
} | |
}; | |
}; | |
var fridge = new Fridge(1000, 10); | |
fridge.enable(); | |
console.log(fridge.checkStatus()); | |
fridge.addFood('APPLE'); | |
console.log(fridge.getFood()); | |
console.log(fridge.getFoodItem('ApPle')); | |
fridge.addFood('beer', 'babana'); | |
console.log(fridge.getFood()); | |
console.log(fridge.getFoodItem('Beer')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment