Unless private variables contain some type of uber-secret personally
damning info, why not just use this?
//JS 1.8
function Point(x,y){
this._x = Math.floor(x);
this._y = Math.floor(y);
}
Point.prototype = {
constructor : Point,
get x() this._x,
set x(p) this._x = Math.floor(p),
get y() this._y,
set y(p) this._y = Math.floor(p),
toString : function() "(" + this._x + "," + this._y + ")"
}
Maybe I'm just not seeing what you're trying to accomplish.
The leading "_" can represent "private" as in "you're not invited to
use this variable", not that "I have a land mine waiting to go off if
you enter".
Alternatively if there is some type of never expose information, I
would suggest a closure over a registry of members mapped to the
"this" variable. Garrett Smith, Andrea Giammarchi, Richard Cornford
and no doubt others have examples of how this approach could be set
up. The one potential downside I see though is that memory has to be
managed manually as a destructor method would be needed to clean up
the registry.
On Jan 1, 5:19 pm, jemptymethod <[email protected]> wrote:
> On Jan 1, 12:48 am, jemptymethod <[email protected]> wrote:
>
> > Please consider the following template. Sure its a little verbose,
> > but Uncle Bob declare comments to be failures, so I'm trying to
> > obviate such failure with the identifiers $private and $public. Also
> > the nested closures allow all state to be hidden with $private, and
> > for code (e.g. $private.init()) to be executed within the same scope
> > from which the ($public) interface is returned.
>
> > Feedback appreciated; I merely intend on opening a discussion. Some
> > or many may disagree with this approach. But is there anything
> > outright wrong about it?
>
> Improved code formatting, with console.log un-commented, and usage of
> the module:
>
> var Module = (function() {
> var $private = (function() {
> var $state = {};
> var $private = {};
>
> $private.state = function() {return $state};
> $private.init = function() {
> console.log('$private.init invoked');
> };
>
> return $private;
> })();
>
> return (function() {
> $private.init();
> $public = {};
> return $public;
> })();
>
> })();
>
> var module = Module;
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]