Minimalist Classes

David Bruant bruant.d at gmail.com
Wed Nov 2 10:35:03 PDT 2011


Le 02/11/2011 18:09, Brendan Eich a écrit :
> On Nov 2, 2011, at 4:10 AM, David Bruant wrote:
>
>> Another topic:
>> -----
>> class Monster {
>>
>>   private name, health;
>>
>>   sameName(other) {
>>     return @name === other at name;
>>   }
>> }
>> -----
>> I am under this impression that you are accessing the private property ("other at name") of an instance which isn't you (other !== this) and I'm not sure it's a good idea.
> Private names do not leak via reflection, not even via proxies. So what's the problem?
My problem is that private names are per-class instead of per-instance.
This is saying that an instance cannot isolate itself from other
instances of the same class.
It doesn't smell good to me in terms of encapsulation.
I am a human being, you are a human being. It doesn't mean we can trust
each other. If I have a password in my head, I don't want you to be able
to access it just because we are both human beings. I want my password
to be safe from foxes, cats, but also other human beings.

>> Is "other" a monster? (how do you "recognize" a monster from any other object?).
> You could do ad-hoc type or shape tests. For the example, and even in most cases in general, there's no need. Duck typing works with private names too.
Who is "you" in your case? My "you" was "runtime". I think runtime
shouldn't do heuristic-tests like comparing shapes.

>> If so, is it a good enough reason for you to be able to access its private state?
> It must be an instance of this class or the name would not be bound.
Is it a good idea that all instances of the same class can access each
others private state? I don't think this is possible in Java, I don't
know an object-oriented language where this is possible. Is there a
precedent of this? Doesn't it break encapsulation?
I think that private names should be "per-instance per-property" rather
than "per-class per-property".

"it must be an instance of this class". I'd like to ask the question
again, how does runtime knows that an object is a instance of a given
class? Will there be an internal property for this? [[Class]]? What is
the value of this internal property for objects not created with classes?

>> What happens in the following case?
>> -----
>> class Monster{
>>   private health;
>>   
>>   constructor(health) {
>>     @health = health;
>>   }
>> }
>>
>> let m = new Monster(100);
>>
>> // pass m to a potentially malicious script:
>>
>> m.kill = function(){
>>   @health = 0;
> health is not in scope here!
Ok, cool :-)

David


More information about the es-discuss mailing list