JSFiddle - React, Tailwind, and code Playground

by Neeraj Yadav

JavaScript

// get number of instances of a class...using a closure

var count = (function() {
  var instances = 0;

  return function() {
    instances += 1;
    console.log("instances are: " + instances);
  }
})();

function robot(name) {
  this.name = name;
  this.getCount = function() {
    count();
  }


  var o1 = new robot("o1");
  o1.getCount();

  var o2 = new robot("o2");
  o2.getCount();