Some questions about Private Name Objects

Matthew Robb matthewwrobb at gmail.com
Mon Aug 27 10:54:09 PDT 2012


I know this has some serious ugly to it but in the spirit of knowing what
COULD potentially create the expected behavior, would this not do the trick?

var myClass = (function(){

        function myClass(){
            var __priv = Object.create(this);
            var __this = __priv.__this = this;

            this.getTest = function(){
               return myClass.prototype.getTest.apply((this
===__this)?__priv:this, arguments);
            }

            __priv.test = 0;
        }

        myClass.prototype.getTest = function() {
          var __this = this.__this || this, __private = this;
          return __private.test;
       }

        return myClass;
    })();

On Mon, Aug 27, 2012 at 11:43 AM, David Bruant <bruant.d at gmail.com> wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120827/0b601968/attachment.html>


More information about the es-discuss mailing list