JSFiddle - React, Tailwind, and code Playground
JavaScript
function PrivateRuns(startOff) {
function getRuns() {// private function used to get private variables
privateRuns -= 1; // accessable with "out" because in same scope
privateRuns = Math.max( privateRuns, 0);
return privateRuns + privateHits;
}
var privateRuns = startOff;
var privateHits = 6;
var out = this; // Used to reference the current scope? ECMA error workaround
this.setRuns = function(val){ privateRuns = Math.max(val, 0); };
this.getAction = function () {// this function is of scope "this"
//out.privateHits = out.privateHits + 1;
return ( "runs n hits: "+ getRuns() +"   " );
};
}
var myRuns = new PrivateRuns(10);
var theirRuns = new PrivateRuns(5);
myRuns.privateRuns = -10;// does not work
//myRuns.setRuns(-10);// does work
for( var i = 0; i < 10; i ++ ){
$("HTML").append( "me: " + myRuns.getAction() +"   " );
$("HTML").append( "them: " + theirRuns.getAction() +"<br>" );
}