Minimalist Classes

David Bruant bruant.d at gmail.com
Wed Nov 2 12:24:01 PDT 2011


Le 02/11/2011 19:29, Brendan Eich a écrit :
> On Nov 2, 2011, at 11:17 AM, David Bruant wrote:
>
>>> See my reply to Kam. We're not sugaring instance-private ivars. I am
>>> proposing something we agreed to in Nov. 2008: sugaring
>>> class-private ivars.
>> Ok, that's what I was missing. What were the rationale? use cases?
>
> The rationale is that most mainstream OO languages with the most users
> support class-private not instance-private ivars. Sorry, Smalltalkers!
I thought Java had per-instance private properties, but not, it's
per-class as well. Interesting.

>
>>> You can make instance-private ivars yourself in the constructor
>>> using Name.create, and go to town. Knock yourself out! (I mean that
>>> in a good way, at least from where I sit :-P).
>> Hmm... Are you sure you can implement instance-private variables with no
>> leak?
>
> You'd need to use the closure pattern:
>
> class BnD {
>   constructor(x) {
>     const my_x = Name.create('x');
>     this[my_x] = x;
>     this.method1 = function (...) {...};
>     ...
>     this.methodN = function (...) {...};
>   }
> }
>
> The method1..N functions can use my_x, no one else can.
Ok. Perfect.
It's unfortunate to loose the syntactic sugar ("this.method1 =
function(){};" and compulsory explicit |this| in the function body), but
it's doable.

Thanks,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111102/937933fd/attachment.html>


More information about the es-discuss mailing list