> I personally prefer method 1 since it seems to allow a notion of public
> and private members. For example,
>
> function myclass() {
> var privateVar = "hi"
> function privateFn() { return privateVar }
> this.publicFn = function() { return privateFn() }
> }
As explained in my previous post, it is irrelevant whether you need an
emulation of protected scope for your method or not; what is relevant
is whether you need per-instance protected scope or just one protected
scope for all instances (so their getter/setter methods will work on
competition basis). The latter is rather dangerous doing because
JavaScript doesn't have synchronized modifier so the execution flow can
get really fancy

In any case it is yours to decide.
1) Per-Instance protected scope (string variable in this sample)
<script type="text/javascript">
function MyObject(arg) {
// Overriding toString method to prevent
// source dump. This method is static so
// shared by constructor and all instances:
this.toString = MyObject.toString;
// Create instance-specific protected scope
// and provide getter/setter accessors to the
// current instance:
ProtectedScope.call(this, arg);
function ProtectedScope(arg) {
this.getPrivate = getPrivate;
this.setPrivate = setPrivate;
var $default = 'foo';
var $private = (typeof arg == 'string') ? arg : $default;
function getPrivate() {
return $private;
}
function setPrivate(arg) {
$private = (typeof arg == 'string') ? arg : $default;
}
}
}
MyObject.toString = function() {
return 'function';
// ... or some bogus function body code
}
var obj1 = new MyObject('foobar');
var obj2 = new MyObject;
obj1.setPrivate('bar');
alert(obj1.getPrivate()); // 'bar'
alert(obj2.getPrivate()); // 'foo' (default)
</script>
There is few catches in here.
First of all, inner functions (inner classes in some other languages)
is what is called "undocumented language feature". It is entirely based
on the fact that any good written engine will try to interpret any code
as long as it's syntactically correct. At the same time while making
the original JavaScript specs no one in the wildest dream wouldn't
imagine a code like function a(){function b(){function c(){}}} not only
being used - but being nearly a core of many solutions. It doesn't mean
do not use it: but it means use with care and check every outcome on
every engine of your interest.
Secondly, as Jonas Raoni already pointed out, regular JavaScript
doesn't have private or protected modifiers. So the max one can do is
to emulate it by carefully applying closure mechanics. In the sample
above despite "visually" we are dealing with the same inner function
ProtectedScope, in fact we are forming new closure (thus separate
scope) for each object instance.
AFAICT Cornford-Crockford (CC) scope management doesn't allow purely
static protected members: that is an implication of a solution based on
closures. The max you can do is to have static compound property
("protected variable" in CC) which is shared among all class
instances: but getter/setter for this property ("privileged methods" in
CC) will be individually cloned for each class instance.
P.S. "class instance" term is used in application to the objects
produced by function-constructor.