Some questions about Private Name Objects

David Bruant bruant.d at gmail.com
Mon Aug 27 08:43:58 PDT 2012


Hi Matthew,

Sorry, I mislead you. Let me retry:

     class myClass{
         private test;

         constructor(){
             this[test] = 0;
         }

         getTest(){return this[test]};
     }

desugars to:

     var myClass = (function(){
         var test = new Name();

         function myClass(){
             this[test] = 0;
         }

         myClass.prototype.getTest = function(){return this[test]};

         return myClass;
     })();

I'm not up-to-date on exact syntax, but that's more or less what you'd get.
An interesting point you're bringing is that the desugared version could 
just use variables in method scopes instead of private names. It could 
yield in this alternative desugaring:

     var myClass = (function(){
         var test;

         function myClass(){
             test = 0;
         }

         myClass.prototype.getTest = function(){return test};

         return myClass;
     })();

It can be read in the code why it doesn't work. There is a unique 'test' 
variable for all instances and not a test variable for each, this makes 
the "getTest" method pointless in this example (because it gets the last 
set value and not the value of the current instance). Being able to 
attach private data to individual objects and being able to access this 
data in inherited method is one of the use case that require private names.

I hope I have make things more clear.

David


More information about the es-discuss mailing list