Minimalist Classes

David Bruant bruant.d at gmail.com
Wed Nov 2 04:10:47 PDT 2011


Le 02/11/2011 07:53, Brendan Eich a écrit :
> On Nov 1, 2011, at 10:27 PM, David Flanagan wrote:
>
>> 4) Do your class bodies allow getters and setters?
>
> Sure, they're easy. Left 'em out for parity and brevity. They follow
> the property initialiser in object literal pattern.
They would be great especially in combinaison with the private syntax

>
>> Your private instance variables are quite interesting.
>
> Yes, that is a missing ingredient in most proposals we've seen.
Indeed. I was already seeing myself writing:
----
{
  let name = Name.create(),
       health = Name.create();
// ...
  class bla{
     method1(a){
       this[health] = a;
     }
  }
}
----
which would have been annoying as hell. If there is a class syntax, it
needs proper instance private properties syntax.

I'm not a huge fan of prefixed name for private instances, but after
deeper thought, its seems unavoidable. I prefer @ to Dart's _ for the
only reason that @ is currently forbidden in JavaScript identifiers,
preventing all confusion.


>
>> But more to the point, you've defined a syntax that allows us to drop
>> 'this' from our methods!  That's seriously cool.
>
> Only for private vars.
Within a class method definition, if an identifier is undeclared,
couldn't it be bound to the public instance property the method is
called on?
... wait a minute. That's a terrible idea! Let's keep it to only private
properties and with a prefix!


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?


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;
}
m.kill();
-----
Or, how do you prevent someone who has access to the object to define a
function which accesses the private state?
Is the @-syntax only allowed within a class body?

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


More information about the es-discuss mailing list