Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Ticker(tickerId, baseDelay=3000, ticksCount=10)
- {
- //let this = {};
- console.log("Ticker works hard with new! :)");
- this.tickerId = tickerId;
- this.delay = baseDelay;
- this.ticksCount = ticksCount;
- this.cnt = 0;
- this.id;
- Ticker.prototype.parentProperty = 42;
- Ticker.prototype.tickerArr = () => {
- console.log(`Ticker #${this.tickerId} ${this.cnt}`);
- (this.cnt++) % 2 ? this.delay *= 2 : this.delay /= 2;
- if (this.cnt == this.ticksCount && this.ticksCount != -1) { clearTimeout(this.id); }
- else { this.id = setTimeout(this.tickerArr, this.delay)};
- };
- Ticker.prototype.armArr = function()
- {
- this.cnt = 0;
- this.id = setTimeout(this.tickerArr, this.delay);
- }
- Ticker.prototype.arm = function()
- {
- this.cnt = 0;
- this.id = setTimeout(function ticker(obj){
- console.log(`Ticker #${obj.tickerId} ${obj.cnt}`);
- (obj.cnt++) % 2 ? obj.delay *= 2 : obj.delay /= 2;
- if (obj.cnt == obj.ticksCount && obj.ticksCount != -1) { clearTimeout(obj.id); }
- else { obj.id = setTimeout(ticker, obj.delay,obj)};
- },this.delay,this);
- };
- Ticker.prototype.disarm = function()
- {
- clearTimeout(this.id);
- };
- Ticker.prototype.set = function(baseDelay=3000, ticksCount=10)
- {
- this.delay = baseDelay;
- this.ticksCount = ticksCount;
- };
- Ticker.prototype.get = function()
- {
- console.log(`Ticker #${this.tickerId}\nDelay: ${this.delay}\nTicksCount: ${this.ticksCount}`);
- }
- //Object.setPrototypeOf(this,Ticker.prototype)
- //return this;
- };
- class Ticker
- {
- constructor(tickerId, baseDelay=3000, ticksCount=10)
- {
- this.tickerId = tickerId;
- this.delay = baseDelay;
- this.ticksCount = ticksCount;
- this.cnt = 0;
- this.id = 0;
- }
- output()
- {
- console.log(`Ticker #${this.tickerId} ${this.cnt}`);
- }
- ticker()
- {
- this.output();
- (this.cnt++) % 2 ? this.delay *= 2 : this.delay /= 2;
- if (this.cnt == this.ticksCount && this.ticksCount != -1) { clearTimeout(this.id); }
- else { this.id = setTimeout(()=>{this.ticker()}, this.delay)};
- };
- arm()
- {
- this.cnt = 0;
- this.id = setTimeout(()=>{this.ticker()},this.delay);
- };
- disarm()
- {
- clearTimeout(this.id);
- };
- set(baseDelay=3000, ticksCount=10)
- {
- this.delay = baseDelay;
- this.ticksCount = ticksCount;
- };
- get()
- {
- console.log(`Ticker #${this.tickerId}\nDelay: ${this.delay}\nTicksCount: ${this.ticksCount}`);
- }
- }
- class TickTack extends Ticker
- {
- constructor(...args)
- {
- super(...args);
- }
- output()
- {
- super.output();
- let now = new Date();
- console.log(`Tick-Tack ${String(now.getHours()).padStart(2,0) + ':' + String(now.getMinutes()).padStart(2,0) + ':' +String(now.getSeconds()).padStart(2,0)}`);
- }
- }
- class Shape
- {
- constructor()
- {
- this._sides = [];
- };
- _setSide(sideId, sideLength)
- {
- this._sides[sideId] = sideLength;
- };
- _getSide(sideId, sideLength)
- {
- return this._sides[sideId];
- };
- sidesInfo()
- {
- this._sides.forEach((it,id)=>{console.log(`Side #${id} is ${it} long`);});
- };
- getPerimeter()
- {
- return this._sides.reduce((prev,cur)=>{return prev+cur;},0)
- };
- getArea()
- {
- return 0;
- };
- get name()
- {
- return this.constructor.name;
- };
- }
- class Rectangle extends Shape
- {
- constructor(sideA, sideB)
- {
- super();
- this._setSide(0,sideA);
- this._setSide(1,sideB);
- };
- getPerimeter()
- {
- return super.getPerimeter() * 2;
- };
- getArea()
- {
- return this._getSide(0) * this._getSide(1);
- }
- }
- class Square extends Rectangle
- {
- constructor(sideA)
- {
- super(sideA,sideA);
- };
- }
- class Triangle extends Shape
- {
- constructor(sideA, sideB, sideC)
- {
- super();
- this._setSide(0,sideA);
- this._setSide(1,sideB);
- this._setSide(2,sideC);
- };
- getArea()
- {
- let p = this.getPerimeter() / 2;
- return Math.sqrt(p*(p-this._getSide(0))*(p-this._getSide(1))*(p-this._getSide(2)));
- }
- }
- let shapes = [new Rectangle(4,5), new Square(6), new Triangle(3,4,5)];
- shapes.forEach((it,id)=>{console.log(`${id}.${it.name} Perimeter: ${it.getPerimeter()} Area: ${it.getArea()}`); it.sidesInfo();});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement