Hi comp.lang.javascript,
I'm throwing this in for discussion. First up I don't claim to be any
sort of authority on the ecmascript language spec - in fact I'm a
relative newb to these more esoteric uses (abuses?) of the language.
I've been working from the oft quoted resource
http://www.crockford.com/javascript/private.html. During my first
serious attempt at using the knowledge acquired from this page, I ran
up against the problem that I couldn't use the 'this' variable in a
public/priviileged function when it is called as a functor by another
object. While trying (unsucessfully) to get this to work without having
to resort to a that/self/me" variable, I stumbled across the fact that
the way I was declaring my 'public' methods meant that they could do
with them everything which is mentioned as motivation for the creation
of 'privileged' methods - 'private' methods and variables were
available in scope of my 'public' methods.
Some code which demonstrates what I'm talking about:
function PseudoClass( ){
var self = this;
self.publicVariable = "'I am public'";
var privateVariable = "'I am private'";
PseudoClass.prototype.Run = function( ){
privateFunction( );
self.privilegedFunction( );
self.publicFunction( );
}
PseudoClass.prototype.publicFunction = function( ){
window.alert( "Public Function called" );
window.alert( "privateVariable = " + privateVariable + " in
public scope" );
window.alert( "self.publicVariable = " + self.publicVariable +
" in public scope" );
otherPrivateFunction( );
self.otherPrivilegedFunction( );
self.otherPublicFunction( );
}
PseudoClass.prototype.otherPublicFunction = function( ){
window.alert( "Other Public Function called" );
}
self.privilegedFunction = function( ){
window.alert( "Privledged Function called" );
window.alert( "privateVariable = " + privateVariable + " in
privileged scope" );
window.alert( "self.publicVariable = " + self.publicVariable +
" in privileged scope" );
otherPrivateFunction( );
self.otherPrivilegedFunction( );
self.otherPublicFunction( );
}
self.otherPrivilegedFunction = function( ){
window.alert( "Other Privledged Function called" );
}
function privateFunction( ){
window.alert( "Private Function called" );
window.alert( "privateVariable = " + privateVariable + " in
private scope" );
window.alert( "self.publicVariable = " + self.publicVariable +
" in private scope" );
self.otherPublicFunction( );
self.otherPrivilegedFunction( );
otherPrivateFunction( );
}
function otherPrivateFunction( ){
window.alert( "Other Private Function called" );
}
}
var testcase = new PseudoClass( );
testcase.Run( );
This works without problems in IE6 SP1 and Opera 8.54. Haven't tested
anything else as of yet. What seems to be different from what others
are doing is that I'm declaring my 'public' methods *inside* my class
definition, thus making everything which is in that scope available to
those methods.
So my questions:
* Is this allowable according to the ecmascript spec?
* Is there something dumb/obvious that I'm missing why what I'm doing
is "a bad idea"?
* Are 'privileged' methods redundant in the light of this?
Thanks for your thoughts/comments
Rhys