How can a prototype method access private variables (values accessed via closures) in the constructor without exposing public getters and setters.
function User() {
var value = 1;
this.increment = function () {
value++;
};
this.set = function (val) {
value=val;
};
this.get = function () {
return value;
};
}
User.protptype.add = function (value) {
this.set(this.get()+value);
}
How can I get rid of get() and set() and still have only one copy of add()?
The intent is to ensure that there is only one instance of the add() function and not one created for each object while that object being able to access the private variables in this case the value variable in the constructor.