One way to make your values referenceable without exposing to each of them directly on your instance is like this
function Foo(b) {
var env = {}; // Object to hold variables
this._getEnvironment = function () { // might want to make this non-enumerable
return env;
};
env.bar = b; // example
}
Foo.prototype.getBar = function () { // inherited, shared
var env = this._getEnvironment();
return env['bar'];
};
var x = new Foo(1),
y = new Foo(2);
x.getBar(); // 1
y.getBar(); // 2
// but remember
x._getEnvironment()['bar']; // 1, still reachable
Now yes you have a different _getEnvironment
function for each instance, but all of the other functions are now shared
I used an Object env
because this is a safe way to look up a value and property look ups are done with a hash table so extremely fast
JavaScript is never really secure, so whilst you can use closures to make it more difficult to access, don't believe that someone motivated wouldn't be able to get around any security measure you try to implement