I came up with one workaround that I don't really like
function Foo(){
this._bar = "12345";
}
Foo.prototype.setBar = function(bar){
if(bar){
this._bar = bar;
}
}
Foo.prototype.getBar = function(){
return this._bar;
}
Foo.prototype.baz = function(){
var bob = "12345";
}
The disadvantages of this are much more unnecessary code - and I really don't understand why a class should always be forced to use accessor for its private variables.
Note. If you try to have a single function, Getter / Setter will not go anywhere.
Foo.prototype.bar = function(bar){
if(bar){
this._bar = bar;
}
return this._bar;
}
Foo.prototype.baz = function(){
this.bar("12345");
this.bar();
}
source
share