Private Names and Methods

Herby Vojčík herby at mailbox.sk
Fri Apr 6 12:31:47 PDT 2012



Brendan Eich wrote:
> Russell Leggett wrote:
>> And would result in copying all of the properties from myMixin into
>> the object literal.
>
> Including the private-named properties? That would be bad for integrity:
> Alice can't give Bob an object-as-capability where the private names
> stay private. Bob can extract them via this hypothetical
> spread-in-braces and then abuse them (e.g. to brand a counterfeit object
> and fool Carol into thinking it was from Alice).

In any decently powerful trait composition system, you have to allow 
copying private-named properties (if only for class prototypes, but if 
it is possible for them, people will other abuse it or there should be 
ability ot do it in more general fashion)>

I do not want spread for objects (it is really too much, I understand, 
though once I also thought it is a good idea), but I think something 
that DRYs .{} should be there.

Let me recall one of my suggestions from before (which was tl;dr-ed 
becuase it was long):

1. add 'f(x,y)' as a possibility into object-initializer (comma 
delimited) as well as recently-proposed max-min classes 
(semicolon-delimited*), for example:

   let obj = { a:2, b:4, prop("c", 6), counter() };
   class Foo {
     constructor(x) { this.{ prop("x", x) } }
     counter()
   }

* note: I still see max-min class basically as an object-initializer for 
.prototype, just with some elements banned and other mapped to slightly 
different syntax; so this is also just a mapping of sort.

2. In the spirit of * for generators, allow function% definitions (and 
concise methods) for data-structure (having object-initializer syntax in 
{ body } block**), to be used as delineated above:

   private x;
   function% prop (name, initial) {
     @x: initial,
     get [name] () { return this. at x; },
     set [name] (v) { this. at x = v; }
   }

   private cnt;
   function% counter() {
     inc () { if (!this. at cnt) { this. at cnt = 1; } else { ++this. at cnt; } },
     current () { return this. at cnt || 0; }
   }

** or using the mapping mentioned in * footnote, but then it must be 
elaborated how it is used for data-properties (and people would ask why 
they are not usable in classes).
------

Would it be (more) ok to allow such pre-built block be included in 
objects even if they carry private properties? I somehow think this is 
inevitable, sooner or later.

This does not allow to copy from any object, only from pieces that were 
specified as usable for composition.

> /be

Herby

P.S.: Of course, if in any level of "call" a property or getter is 
encountered from within max-min class definition, it should throw an 
error, this is by no way meant as a way to sneak data-definition into 
classes which prohibit this in the current state.


More information about the es-discuss mailing list