Advertisement
TeeZ0NE

coffeMachine, User. Public/Private

Jan 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. function PUser(name) {
  3.   "use strict";
  4.   let rname = name;
  5.   const sbue = 'Bue! ';
  6.   this.sayHi = () =>
  7.   console.log('hey ' + rname, bue());
  8.   //this.setName = (name) => rname = name;
  9.   Object.defineProperty(this, 'rname', {
  10.     get: () => rname,
  11.     set: (name) => {rname = name;}
  12.   });
  13.   let bue = ()=>`${sbue}${rname}`;
  14. }
  15. var nuser = new PUser('Vas');
  16. nuser.sayHi();
  17. // nuser.setName('sobol');
  18. nuser.rname = 'sobol';
  19. nuser.sayHi();
  20. console.log(nuser.rname);
  21.  
  22. /**
  23. * CoffeeMachine constructor
  24. * @param {integer} power
  25. * @param {integer} capacity
  26. */
  27. function CoffeeMachine(power, capacity) {
  28.   'use strict';
  29.   if (!isFinite(power) || !isFinite(capacity)) throw new Error('Power and Capacity mast be a Integer')
  30.   let waterAmount = 0;
  31.   let timerId;
  32.   let self = this;
  33.   let running = false;
  34.   this.run = () => {
  35.     console.log(`Is running: ${running=true}`);
  36.     timerId = setTimeout(onReady, getBoilTime());
  37.   }
  38.   this.stop = () => {
  39.     clearTimeout(timerId);
  40.     running = false;
  41.     console.log('Stopped!');
  42.   };
  43.   //getter/seter via method
  44.   this.wa = (amount = 0) => {
  45.     if (!amount) return waterAmount;
  46.     waterAmount = amount;
  47.   };
  48.   this.addWater = (addWater)=>{
  49.     let newAmount = waterAmount + addWater;
  50.     if (newAmount<0 || newAmount>capacity) throw new Error("Check water add count");
  51.     else waterAmount = newAmount;
  52.   }
  53.   //getter/setter via property
  54.   Object.defineProperties(this, {
  55.     "power": {
  56.       get: () => {
  57.         return power;
  58.       }
  59.     },
  60.     "running": {
  61.       get: ()=>{return running;}
  62.     },
  63.     "waterAmount": {
  64.       get: () => waterAmount,
  65.       set: (amount) => {
  66.         switch (amount) {
  67.           case (amount < 0) :
  68.             throw new Error('Less then 0');
  69.             break;
  70.           case (amount > capacity) :
  71.             throw new Error('More then capacity');
  72.             break;
  73.           default:
  74.             waterAmount = amount;
  75.         }
  76.       }
  77.     }
  78.   });
  79.   let getBoilTime = function () {
  80.     const c = 4200;
  81.     const dT = 80;
  82.     return c * waterAmount * dT / power;
  83.   }; //.bind(this);
  84.   function onReady() {
  85.     console.log('Coffee ready!');
  86.     running = false;
  87.   }
  88. }
  89. var coffeeMachine = new CoffeeMachine(100000, 300);
  90. coffeeMachine.waterAmount = 200;
  91. coffeeMachine.addWater(100);
  92. console.log(coffeeMachine.waterAmount + ' ' + coffeeMachine.power);
  93. // coffeeMachine.wa(219);
  94. console.log('Remind ', coffeeMachine.wa());
  95. coffeeMachine.run();
  96. console.log(coffeeMachine.running);
  97.  setTimeout(function(){console.log(coffeeMachine.running)},3000);
  98.  
  99. /**
  100. * User constructor with private and public methods
  101. */
  102. function User(){
  103.   let firstName, surName;
  104.   this.getFullName = ()=> `${firstName} ${surName}`;
  105.   Object.defineProperties(this,{
  106.     firstName:{
  107.       set: (fName)=>{firstName = fName;}
  108.     },
  109.     surName:{
  110.       set: (sName)=>{surName = sName;}
  111.     }
  112.   });
  113. }
  114.  
  115. var user = new User;
  116. user.surName = "Doe";
  117. user.firstName = "John";
  118. console.log(user.getFullName());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement