es-discuss Digest, Vol 46, Issue 22
Brendan Eich
brendan at mozilla.com
Tue Dec 21 15:40:02 PST 2010
On Dec 21, 2010, at 2:52 PM, thaddee yann tyl wrote:
> I was very interested when I heard about private names. It seems like
> a very good idea. However, the specification that the strawman gives
> is harder than I thought it would be.
>
> I am unsure I fully understand this _private names_ proposal.
>
> For starters, what is the "scope" of private names?
This is spec'ed at http://wiki.ecmascript.org/doku.php?id=strawman:private_names#private_declaration_scoping to some level of precision:
"private declarations are lexically scoped, like all declarations in Harmony. Inner private declarations shadow access to like-named private declarations in outer scopes. Within a block, the scoping rules for private declarations are the same as for const declarations. "
> eg,
>
> private name;
> var obj = {};
> obj.name = 5; // We want obj to have a Private Name value.
> // ... Later on ... (here, we should do something so that the
> // following lines work).
> var foo = {};
> foo.name = {name:'foo'}; // Here, we would like foo to have a
> // property "name" whose value is an object whose name property holds the
> // value "foo".
> // How to do so? Is foo['name'] = {'name':'foo'}; unavoidable; does it
> // even work?
> // Is {name:'foo'} going to contain a Private Name, or rather a
> // property (which is what we want)?
Yes, if you don't close the block scope (even if implicit, top-level script or function body scope) then |private name| is still in effect.
If you want {name: 'foo'} to mean {"name": 'foo'}, then either close the block scope (open a fresh one first if necessary), or quote the identifier. Same for foo.name (foo["name"]).
> Secondly, I am trying to understand what this code does, too:
> a = {};
> k = {a: a};
> a['k'] = k;
>
> function aa(o) {
> private a;
> k.a = o; // Does it "override" the fact that a is a property?
I don't know what you mean by "override". The private_names strawman is clear enough about what happens here. k has a new property named by the private name lexically bound to a in function aa, and *not* named by the string "a", created by assignment and set to have as its value the value of |o|.
> a.a = a.k.a;
> return #.a;
> }
>
> var a1 = aa(a);
> var a2 = aa(a); // Does it return the same thing?
No, you get a fresh one per evaluation of the private declaration. See
function Thing() {
private key; // each invocation will use a new private key value
and related examples from http://wiki.ecmascript.org/doku.php?id=strawman:private_names#using_private_identifiers.
As one might hope, you can nest closures to make "class private" vs "instance private" names. There's no need for extra keywords or special cases. A private declaration is evaluated by generating a new, unique name.
> print(a2);
> print(a1 === a2);
false
> print(typeof a2);
The strawman answers this, but includes words about an alternative design wherein private names are built-in objects.
> print(a[a2] === a.k.a);
false
Note that (a[a2] === a.k[a2] && a[a2] === a).
This is all kind of plug-and-chug from my reading of the strawman. Was it unclear in a way where you could suggest a fix? Or maybe it just didn't start with a minimal statement of the rules.
/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20101221/571307d9/attachment.html>
More information about the es-discuss
mailing list