Erik Arvidsson has posted an article about instance private, class private, packages and friends.
Erik stated the traditional methods of setting private members in JavaScript are having some shortcomings:
function A() {
var privateMember = "value";
this.getMember = function() {
return privateMember;
}
}
So what should we do? Erik suggested the key point is actually think seriously about whether the members should be truly private or not:
If you really need to be able to hide the state then I would use the instance private pattern first used in this post. It is easy to write and easy to understand and does not have a lot of boiler plate code. However, I would only use it where hiding the state really matters. In most cases it doesn’t. For example, Gmail was written without any true private members. We just use a naming convention.
no comments
RSS / trackback