There is one problem with Vitalyโs answer, and you cannot define the variables that you want to be unique for the region, if you made this name this way and then changed it, the value of the name will change for each individual instance of the class, so there is one way to solve this problem.
Cannot access an array of names from outside unless you use getNames
check this
test = new MyClass; tempNames = test.getNames() tempNames # is ['joe', 'jerry'] # add a new value tempNames.push 'john' # now get the names again newNames = test.getNames(); # the value of newNames is now ['joe', 'jerry', 'john'] # now to check a new instance has a new clean names array newInstance = new MyClass newInstance.getNames() # === ['joe', 'jerry'] # test should not be affected test.getNames() # === ['joe', 'jerry', 'john']
Compiled Javascript
var MyClass; MyClass = function() { var names; names = ['joe', 'jerry']; MyClass = (function() { MyClass.name = 'MyClass'; function MyClass() {} MyClass.prototype.getNames = function() { return names; }; return MyClass; })(); return new MyClass; };
iConnor Feb 24 '14 at 3:02 2014-02-24 03:02
source share