static/class properties

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Thu Sep 19 21:20:59 PDT 2013


On Sep 19, 2013, at 6:30 AM, Rick Waldron wrote:

> On Thu, Sep 19, 2013 at 4:16 AM, Dmitry Soshnikov <dmitry.soshnikov at gmail.com> wrote:
> Hi,
> 
> Out of curiosity: we have static methods, but seems there is no yet ability to define a class/static property/constant.
> 
> class A {
>   const VALUE = 10;
> }
> 
> or (probably better to be consistent with static methods):
> 
> class A {
>   static VALUE = 10;
> }
> 
> 
> 
> A decision would have to be made for "const VALUE = 10" here: does it mean mean `A.VALUE` and not `var a = new A(); a.VALUE;`?

Yes, it's true, that if a property is placed in the prototype, it can be treated sort of a "static", i.e. shared between all instances. However, I'd assume static foo = 10; means the same as static foo() {}, that is -- placed on the class object itself.


> If it means the latter, then there are implications that will change a possible future that includes "private x = 1" in the same class-body position.
> 

In contrast, "private" modifier should declare a property per instance of course.

> I spoke with Allen off-line and he reminded me that a static "const-like" could be created like this:
> 
> class A {
>   static get VALUE() { return "some constant value"; }
> }
> 

Yeah, but this is "too unsugared" for simple class consts. For accessor consts still work though.

So, if it's not that hard to add to the spec, I'd argue for:

class A {
  static foo = 10; // or const foo = 10; whatever
}

desugars to:

A.foo = 10;

> 
> 
> function getHeavyValue() {
>   static heavyValue;
>   if (!heavyValue) {
>     // heavy calc
>   }
>   return heavyValue;
> }
> 
> 
> This is a no-go because "static" is only reserved in strict mode, which means 
> 
> function foo() {
>   static = 1;
> }
> 

OK.

Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130919/da4cc898/attachment.html>


More information about the es-discuss mailing list