How is it with the "private(this)"?

Herby Vojčík herby at
Wed Dec 21 03:50:39 PST 2011


I see some discrepancies between dynamic slot-based ES versus concept of
"private per-object state".

The main problem seems to be that I can do this:

  (function() { return private(this).secret; }).call(obj);

In that case, I can read (and also write) object's private property at will.
But how is it ten private?

The problem I see is in notion of "per-object" - if you want the property be
private for any method of an object, but not accessible "from outside", you
can make anything method of an object and access it. It may be the case of though I hope it is not, but it seems to
me the only conceivable method of "private state" is "private per-scope
state", and I mean scope more broadly - not a function scope, but a "bunch
of methods"-scope. Because, it seems to me, first we should ask the question
"why do we want private?" and the answer seems to be "to have state that can
only be accessed and modified by narrow subset of code" and I strongly
believe that this "narrow subset of code" is always closed, not open, that
is, it is a bunch of methods (and constructors, and static methods) that
realize certain functionality where they need to share some state, but this
state is _not_to_be_disclosed_ to anyone else (even other methods of the
same class added later, or subclasses, or superclasses, or the ad-hoc method
from beginning of the post).

That leads me to the conclusion (and here it may be I fell victim to the
Blub syndrome) that only natural "private" is the (lexical; but lexical only
because it is easy way of grouping methods that will share access to it, not
lexical per se) scope private; in the present state of class proposal, the
private that can be accessed by all methods in class definition block, but
no other.

Please clear my mistakes, if you find any :-)

I'd like to see real "per-object private" use case for such dynamic language
as ECMAScript is. Thank you,


More information about the es-discuss mailing list