Skip to content

Instantly share code, notes, and snippets.

@severuykhin
Created April 2, 2017 13:33
Show Gist options
  • Save severuykhin/ebec18431baec9f47a4fc42c716ea98b to your computer and use it in GitHub Desktop.
Save severuykhin/ebec18431baec9f47a4fc42c716ea98b to your computer and use it in GitHub Desktop.
JavaScript OOP examples
function CoffeMachine(power){
const _this = this; //Сохранение контекста для обрращения к нему из внутренних функций
//Set water amount
this.waterAmount = 0;
const WATER_HEAT_CAPACITY = 4200;
var timerId;
//Count time to boil water
var getBoiledTime = function () {
var time = _this.waterAmount * WATER_HEAT_CAPACITY * 80 / power;
console.log(time);
return time;
};
//Action when coffie is ready
function onReady(){
console.log('Кофе готов');
}
//Run coffie machine
this.run = function(){
timerId = setTimeout(onReady, getBoiledTime());
};
this.stop = function () {
clearTimeout(timerId);
}
}
var machine1 = new CoffeMachine(600);
var machine2 = new CoffeMachine(300);
machine1.waterAmount = 10;
machine1.run();
machine1.stop(); //Nothing happen
machine2.waterAmount = 20;
machine2.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment