Created
April 2, 2017 15:03
-
-
Save severuykhin/4c9df98175fe4304306202164caca56c to your computer and use it in GitHub Desktop.
JavaScript OOP examples w
This file contains hidden or 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
| //Coffie machine constructor | |
| //=================================================================== | |
| function CoffeMachine(power, capacity){ | |
| //Define private vars //Сохранение контекста для обрращения к нему из внутренних функций | |
| const WATER_HEAT_CAPACITY = 4200; | |
| const POWER = power; | |
| var timerId; | |
| var isRunning = false; | |
| //Define default water amount | |
| var waterAmount = 0; | |
| //Set water amount (setter in action) | |
| this.setWaterAmount = function(amount){ | |
| if (amount < 0) { | |
| throw new Error ('Значение должно быть положительным'); | |
| } | |
| if (amount > capacity) { | |
| throw new Error (`Воды должно быть меньше ${capacity}`); | |
| } | |
| waterAmount = amount; | |
| }; | |
| //Public method for showing water amount | |
| this.getWaterAmount = function(){ | |
| return waterAmount; | |
| }; | |
| var _this = this; | |
| console.log(_this); | |
| //Count time to boil water | |
| var getBoiledTime = function () { | |
| var time = waterAmount * WATER_HEAT_CAPACITY * 80 / power; | |
| return time; | |
| }; | |
| this.getBoiledTime = function (){ | |
| return getBoiledTime(); | |
| } | |
| //Run coffie machine | |
| this.run = function(){ | |
| isRunning = true; | |
| timerId = setTimeout(function(){ | |
| onReady(); | |
| }, getBoiledTime()); | |
| }; | |
| this.stop = function () { | |
| clearTimeout(timerId); | |
| }; | |
| this.getPower = function(){ | |
| return POWER; | |
| }; | |
| this.isRunning = function(){ | |
| return isRunning; | |
| }; | |
| var onReady; | |
| //Action when coffie is ready | |
| this.setOnReady = function(func){ | |
| if (typeof func != 'function') {throw new Error ('Укажите функцию')}; | |
| onReady = func; | |
| } | |
| } | |
| var machine1 = new CoffeMachine(1000, 500); | |
| machine1.setWaterAmount(10); | |
| console.log(machine1.isRunning()); //False | |
| machine1.run(); | |
| machine1.setOnReady(function(){ | |
| var time = machine1.getBoiledTime(); | |
| var amount = machine1.getWaterAmount(); | |
| console.log( `Кофе готов: ${amount} мл за ${time / 1000} секунд` ); | |
| }); | |
| console.log(machine1.isRunning()); //True | |
| //User constructor | |
| //===================================================================================== | |
| const User = function (){ | |
| //Define private methods | |
| var firstName = null; | |
| var surName = null; | |
| //Define setters for name and surname | |
| this.setName = function(name){ | |
| //Check if no string | |
| if (typeof name != 'string') { | |
| throw new Error('Значение должно быть строчным'); | |
| } | |
| else { | |
| if (name.length <= 1) { throw new Error('Слишком короткое имя') } | |
| if (name.length >= 20) { throw new Error('Слишклм длинное имя') } | |
| firstName = name; | |
| } | |
| }; | |
| this.setSurname = function(surname){ | |
| //Check in not string | |
| if (typeof surname != 'string') { | |
| throw new Error('Фамиля должна быть строкой'); | |
| } | |
| else { | |
| //Check length | |
| if (surname.length <= 2) { throw new Error('Слишком короткая фамилия') } | |
| if(surname.length >= 25) { throw new Error('слишком длинная фамилия') } | |
| surName = surname; | |
| } | |
| }; | |
| this.getFullName = function(){ | |
| return `Имя:${firstName}\nФамилия:${surName}`; | |
| }; | |
| } | |
| var user1 = new User(); | |
| user1.setName('Коля'); | |
| user1.setSurname('Васильев'); | |
| console.log(user1.getFullName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment