Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * parent constructor
- * @param {array} arg - arguments
- */
- function Machine(...arg){
- let _enabled = false;
- let [_power] = arg;
- this.enable = ()=>{_enabled = true};
- this.disable = ()=>{_enabled=false};
- Object.defineProperties(this, {
- "_enabled": {
- get: ()=>{return _enabled;}
- },
- "_power":{
- get:()=>{return _power;}
- }
- })
- }
- /**
- * CoffeeMachine constructor
- * @param {integer} power
- * @param {integer} capacity
- */
- function CoffeeMachine(power, capacity) {
- Machine.apply(this,arguments);
- if (!isFinite(this._power) || !isFinite(capacity)) throw new Error('Power and Capacity mast be a Integer')
- let waterAmount = 0;
- let timerId;
- let self = this;
- let machine_enable = this.enable; //assign parent enablemethod
- // rewrie own method
- this.enable = ()=>{
- machine_enable.call(this);//call parent method enable
- this.run();
- }
- this.run = () => {
- console.log(`Is running: ${this._enabled}`);
- timerId = setTimeout(onReady, getBoilTime());
- }
- this.stop = () => {
- clearTimeout(timerId);
- this.disabled();
- console.log('Stopped!');
- };
- //getter/seter via method
- this.wa = (amount = 0) => {
- if (!amount) return waterAmount;
- waterAmount = amount;
- };
- this.addWater = (addWater)=>{
- let newAmount = waterAmount + addWater;
- if (newAmount<0 || newAmount>capacity) throw new Error("Check water add count");
- else waterAmount = newAmount;
- };
- //getter/setter via property
- Object.defineProperties(this, {
- "waterAmount": {
- get: () => waterAmount,
- set: (amount) => {
- switch (amount) {
- case (amount < 0) :
- throw new Error('Less then 0');
- break;
- case (amount > capacity) :
- throw new Error('More then capacity');
- break;
- default:
- waterAmount = amount;
- }
- }
- }
- });
- let getBoilTime = function () {
- const c = 4200;
- const dT = 80;
- return c * waterAmount * dT / power;
- }; //.bind(this);
- function onReady() {
- console.log('Coffee ready!');
- self.disable();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement