On Jan 26, 1:37*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> err_ wrote:
> > Pointy,
>
> Who, error?
bah, google groups is a pain.
>
> > (I'm sure you probably know this but for the record)
>
> Yes, I do.
>
> > the problem with moving the privates out of the constructor
> > is they act sort of "static" if you put them out there...
> > They act like private statics, which sounds like a contradiction,
> > but look:
>
> ISTM that you don't know what you are talking about.
>
That's okay, I'm confident that I do
> > this.TestA = (function() {
>
> > * // private outside of ctor
> > * var _myPvt = ' do not touch ';
>
> > * // ctor
> > * function TestA (newPvt)
> > * {
> > * * if (newPvt !== undefined)
> > * * * _myPvt = newPvt;
> > * }
> > [...]
>
> Non sequitur. *You *modified* the constructor here so that it modified the
> "private" variable; my code does not do that. *
I could have modified it by adding a setter to achieve the same thing.
> That leaves as the only
> inherent problem with moving the prototype property (not variable)
> assignment out of the constructor, that the prototype property value is not
> reset on construction. *
That was exactly my point -- the value of those variables is shared
between every "instance" of the "class."
this.TestA = (function() {
var _myPvt = ' do not touch ';
function _getMyPvt()
{
return _myPvt;
}
function _setMyPvt(v)
{
_myPvt = v;
}
function TestA ()
{
/*
* buy the assignment with a type test here, or move
* it out of the constructor, if you want
*/
TestA.prototype.getMyPvt = _getMyPvt;
TestA.prototype.setMyPvt = _setMyPvt;
}
/* Assign this like getMyPvt() in the constructor if you want */
TestA.prototype.talk = function() {
window.alert(this.getMyPvt());
};
return TestA;
})();
var foo = new TestA();
foo.talk();
foo.setMyPvt(" whee ");
var bar = new TestA();
bar.talk(); // "whee"
bar.setMyPvt(" whoo ");
foo.talk(); // "whoo"
....
So saying these are anything like private class properties in a
'normal' OO language is misleading. Not that you said that, of course.
-- Nick