JSFiddle - React, Tailwind, and code Playground

by Alexander

JavaScript

console.clear();

{
  let internals = new WeakMap();
  function internal(object) {
      if (!internals.has(object)) internals.set(object, {});
      return internals.get(object);
  }
}

const _counter = Symbol('counter');

class Countup {

   constructor(counter, lim = 100, action) {   		
     	let ths = internal(this);
     	//ths.counter = counter;
      this[_counter] = counter;
		 	ths.lim = lim;
			ths.action = action;
   }
   
   inc() {
   		 let ths = internal(this);

       if (++this[_counter] > ths.lim) return;

			 if (this[_counter] === ths.lim){
       		ths.action();
       }
       else {
       		console.log(`Counter = ${ this[_counter] }`);
       }
   }
 }
 
let c = new Countup(0, 3, ()=> console.log('DONE'));
c.inc();
c.inc();
c.inc();