Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- function PUser(name) {
- "use strict";
- let rname = name;
- const sbue = 'Bue! ';
- this.sayHi = () =>
- console.log('hey ' + rname, bue());
- //this.setName = (name) => rname = name;
- Object.defineProperty(this, 'rname', {
- get: () => rname,
- set: (name) => {rname = name;}
- });
- let bue = ()=>`${sbue}${rname}`;
- }
- var nuser = new PUser('Vas');
- nuser.sayHi();
- // nuser.setName('sobol');
- nuser.rname = 'sobol';
- nuser.sayHi();
- console.log(nuser.rname);
- /**
- * CoffeeMachine constructor
- * @param {integer} power
- * @param {integer} capacity
- */
- function CoffeeMachine(power, capacity) {
- 'use strict';
- if (!isFinite(power) || !isFinite(capacity)) throw new Error('Power and Capacity mast be a Integer')
- let waterAmount = 0;
- let timerId;
- let self = this;
- let running = false;
- this.run = () => {
- console.log(`Is running: ${running=true}`);
- timerId = setTimeout(onReady, getBoilTime());
- }
- this.stop = () => {
- clearTimeout(timerId);
- running = false;
- 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, {
- "power": {
- get: () => {
- return power;
- }
- },
- "running": {
- get: ()=>{return running;}
- },
- "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!');
- running = false;
- }
- }
- var coffeeMachine = new CoffeeMachine(100000, 300);
- coffeeMachine.waterAmount = 200;
- coffeeMachine.addWater(100);
- console.log(coffeeMachine.waterAmount + ' ' + coffeeMachine.power);
- // coffeeMachine.wa(219);
- console.log('Remind ', coffeeMachine.wa());
- coffeeMachine.run();
- console.log(coffeeMachine.running);
- setTimeout(function(){console.log(coffeeMachine.running)},3000);
- /**
- * User constructor with private and public methods
- */
- function User(){
- let firstName, surName;
- this.getFullName = ()=> `${firstName} ${surName}`;
- Object.defineProperties(this,{
- firstName:{
- set: (fName)=>{firstName = fName;}
- },
- surName:{
- set: (sName)=>{surName = sName;}
- }
- });
- }
- var user = new User;
- user.surName = "Doe";
- user.firstName = "John";
- console.log(user.getFullName());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement