Private properties: avoiding reification of names, unit-testing

Brendan Eich brendan at mozilla.com
Sun Oct 2 07:58:36 PDT 2011


On Oct 2, 2011, at 4:43 PM, Axel Rauschmayer wrote:

> http://wiki.ecmascript.org/doku.php?id=harmony:private_name_objects
> 
> I would like to avoid reifying the names of private properties (as variables holding name objects). Wouldn’t it be enough if they were private relative to the current class/constructor? Then the following would work (strawman syntax):
> 
> function MyClass(pwd) {
>    this[private(MyClass, "pwd")] = pwd;

You just reified, in the square brackets.

Worse, you seem to make it possible for anyone with access to MyClass to pass a string into this private pseudo-function (I hope it cannot be passed around as a funarg!) and get out the reified private name!

If you mean only for the private(C,id) form to be valid in [], then surely we should be talking about shorter and less special and wrongly general-looking syntax, e.g. infix @.


> }
> MyClass.prototype.isPwd = function(pwd) {
>    return this[private(MyClass, "pwd")] === pwd;
> }
> 

With the current proposal, you can do something like this:

{
  import create from "@name";
  const pwdName = create("pwd");
  function MyClass(pwd) {
    this[pwdName] = pwd;
  }
  MyClass.prototype.isPwd = function(pwd) {
    return this[pwdName] === pwd;
  }
}

It might be too verbose to have to import create from "@name" and call it. We can address that with sugar or shorter API design. But that's not what you were addressing.

/be

> Another consideration: private methods are sometimes helpers that have to be unit-tested. Thus, one should be able to access them “from outside”. That’s something you can’t do if you use a closure for private data.
> 
> -- 
> Dr. Axel Rauschmayer
> 
> axel at rauschma.de
> twitter.com/rauschma
> 
> home: rauschma.de
> blog: 2ality.com
> 
> 
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111002/d217a669/attachment-0001.html>


More information about the es-discuss mailing list