Are "privileged" methods redundant?



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

.



Relevant Pages

  • Re: INLIST procedure
    ... When you declare a variable PUBLIC or LOCAL, ... a variable with a defined scope, ... the PRIVATE does not do this. ... want a gotcha in code that can be easily avoided. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Alberts MultiSelect Form Question #1
    ... This whole issue centers around what we call scope. ... are private to that ONE MODULE). ... functions and routines within that module (they default to private, ... to be used outside of that module, then I must declare them as public. ...
    (microsoft.public.access.formscoding)
  • Re: Analog & Digital scope reads of Lown wave
    ... I wrote you the private msg to avoid this thread to grow up with non ... conducted when the scope probes are attached. ... To be honest, I prefer a good glass of Lambrusco which is sweeter than ... I prefer white wines to the reds, ...
    (sci.electronics.equipment)
  • RE: Naming conventions for variables
    ... naming convention which guarantees avoiding Key Words as names. ... little evidence of the existence of any STANDARD naming convention in VBA -- ... SCOPE + TYPE + NAME. ... with Private scope might have a name like: ...
    (microsoft.public.excel.programming)