shortcuts for defining block-local private names, plays nicely with @foo syntax

Brendan Eich brendan at
Mon Jan 23 12:27:11 PST 2012

> Herby Vojčík <mailto:herby at>
> January 23, 2012 11:51 AM
> Brendan Eich-2 wrote:
>>> Herby Vojčík<mailto:herby at>
>>> January 23, 2012 3:21 AM
>>> It can be solved this way:
>>>    module {
>>>      private foo, bar;
>>>      export function factory1 (args) { ... }
>>>      export function factory2 (args) { ... }
>>> but then foo and bar are known to the rest of the module. It was not
>>> the premise.
>> There is nothing wrong with this solution, and everything right. If you
>> want to keep foo and bar secret from other modules, you've done so. If
>> you, for some reason, want to keep them secret from other contents of
>> this module, use a block (but you'll have a harder time exporting --
>> export must come at module top level).
> In other way, it is impossible to have truly lexical-local private names.

No, the private binding form that is desugared from

   private foo;


   const foo = Name.create("foo");

or better (with hygiene to use the right Name, etc.) is lexical in its 
binding structure.
> You must define them all at the upper level, tens or maybe hundreds of lines
> back.


We're talking about singletons -- you seemed to agree. Why should deeply 
nested declarations be evaluated only once (at compile time? What does 
that mean exactly? Or somehow first-run-only, at their nesting depth...)?

There's no difference here between other forms, including regular 
expression literals and function expressions and declarations. A 
function declaration in particular is evaluated ot a fresh object bound 
to the declared name. The desugaring is complicated by hoisting, but 
consider a magic hoist primitive:

   function bar(params) {body}

desugars to

   hoist bar = function bar(params) {body};

and you get a fresh object per evaluation.

You want to mislocate private for some reason. Why? Are you focused too 
much on class constructors?

The module with two functions sharing private names use-case is 
addressed by module-scope private declarations. What is the syntax you 
want there?


More information about the es-discuss mailing list