Minimalist Classes

Waldemar Horwat waldemar at google.com
Thu Nov 3 14:46:50 PDT 2011


On 11/02/2011 04: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.
> Is "other" a monster? (how do you "recognize" a monster from any other object?). If so, is it a good enough reason for you to be able to access its private state?
> If other is not a monster, what is the behavior?

Without guards you *don't* know that "other" is a monster.

The above simple case works because "other" won't have an @name property.  However, once you consider assignments, you do have a problem:

class Monster {

private name;

writeName(other) {
   other at name = "cookie";
}

... sameName  ...
}

Now you can create @name properties on any object.  Depending on which of the myriad of class proposals you're considering, you might not even need a method that writes to other at name -- if you can extract a method from a class and give it a different "this", then all you'd need would be any method that writes to @name.

     Waldemar


More information about the es-discuss mailing list