New private names proposal

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Dec 21 19:42:38 PST 2010


On Dec 21, 2010, at 4:01 PM, Oliver Hunt wrote:

> function MyAwesomeThing() {
>    ....
> }
> 
> MyAwesomeThing.prototype.myCoolFunction = function() {
>    if (!this._myCachedHotness)
>        this._myCachedHotness = doExpensiveThing(this)
>    return this._myCachedHotness;
> }
> 
> I see this nifty private names feature, and say "cool! now i can make my cache super secret!" and do:
> 
> MyAwesomeThing.prototype.myCoolFunction = function() {
>    private cachedHotness;
>    if (!this.cachedHotness)
>        this.cachedHotness = doExpensiveThing(this)
>    return this.cachedHotness;
> }
> 
> I would _expect_ this to work.  That's what the syntax makes me think.  But it won't work because 'cachedHotness' is going to be different on every call (at least to my reading).
> 
> I am not trying to argue that making the above work is impossible -- you just need to use a few closures to get everything into the right place.  But it is contrary to what I might expect or want.

I don't think there is any new issue here we don't already have with things like closure captured object state or prototype construction such as:

function awesomeFactory(a,b) {
    const awesomeProto = { ...};  //oops, better move this outside of awesomeFactory!!
    return Object.create(awesomeProto, {a: {value:a}, b: {value:b}});
}

Personally, I think function own (aka static) declarations are a good solution to this problem and could be apply equally well to private name declarations.  However, that's not something that I want to put on the table at this time.

Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20101221/f3c580b9/attachment.html>


More information about the es-discuss mailing list