Some questions about Private Name Objects

Matthew Robb matthewwrobb at gmail.com
Mon Aug 27 07:55:27 PDT 2012


SO it has to be constructed via new Name() or will it automatically create
Name objects when it encounters an assignment of that form? If you do have
to create it does that mean in order to access it at all you would need to
be in scope of myname2?

My question I think boils down to whether access is SCOPE gated or OBJECT
gated:

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

  return myClass;
})()

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

Is the above perfectly valid?

On Mon, Aug 27, 2012 at 10:46 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

> A name object is not much different from using a string:
> let myname1 = "S4u-1_tlzUI";
> let myname2 = new Name();
>
> someobj[myname1] = 123;
> someobj[myname2] = 123;
>
> // In a constructor or method:
> this[myname1] = "abc";
> this[myname2] = "abc";
>
> You can pass both around and create properties on any (non-frozen and
> extensible) object. The main difference is that if you use myname2 then the
> property won’t be detected via the usual means (Object.keys,
> Object.getOwnPropertyNames, in operator, for-in loop, etc.). You need the
> value in myname2 to access properties created via it. But you can let other
> parties know about that value. That’s what makes it impossible to use
> closures to simulate this feature.
>
> On Aug 27, 2012, at 15:34 , Matthew Robb <matthewwrobb at gmail.com> wrote:
>
> So I have been trying to figure out from the wiki precisely how Private
> Name Objects work. When assigned/defined does it expose that property to
> all sibling properties/methods or just those present at the time of
> assign/define? If you add on to the object later to the new properties get
> access?
>
> var Person = {
>   [hunger]: 100,
>   eat: function(){ --this[hunger]; }
> }
>
> ? Person.getHunger = function() { return this[hunger] }
>
> class Person {
>   constructor(){ this[hunger] = 100; }
> }
>
> Person.prototype.getHunger = function(){ return this[hunger]; }
>
> Does it make a difference wether it is a 'method' vs a property with a
> function value?
>
> var Person = {
>   [hunger]: 100,
>   eat: function(){ --this[hunger]; }
>   isHungry: function() { return this[hunger] > 0; }
>   getHunger() { return this[hunger]; }
> }
>
> I'm trying to determine whether when compiling into ES3/5 you could get
> away with not attaching some .__privates__ property to the object and
> instead just put a closure around the definitions.
>
> Thanks ahead of time!
>
> Matthew Robb
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>         --
> Dr. Axel Rauschmayer
> axel at rauschma.de
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120827/5cf78b4a/attachment-0001.html>


More information about the es-discuss mailing list