Some questions about Private Name Objects

Axel Rauschmayer axel at rauschma.de
Mon Aug 27 07:46:02 PDT 2012


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/7ca698f6/attachment.html>


More information about the es-discuss mailing list