static/class properties

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Sat Oct 19 11:27:11 PDT 2013


On Thu, Sep 19, 2013 at 9:20 PM, Dmitry Soshnikov <
dmitry.soshnikov at gmail.com> wrote:

>
> 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;
>
>

Sorry for "beating a dead horse" (actually, I don't think it's what I'm
doing), but this topic was left uncleared, so just wanted to double-check
the status, and possibility of nevertheless including this still in ES6:

Locally, using ES6 classes transform already for a while, we periodically
receive questions from devs like: "OK, we got static methods, cool, but how
do I define a static class const?"

There is no good answer yet, and referring them to ES7 era doesn't sound to
me as a good solution for every-day practical programming, when people need
these constants.

I know there were some discussions and bikeshedding around it, like: 1)
whether it should go to the constructor (of course it should!), or 2) to
the prototype, or 3) even to an instance (that doesn't make much sense,
etc. But the basic form, that is used in other languages, and that is
widely used as an everyday practical pattern, could be easily added to the
ES6 spec:

```
var request = new Request;

if (request.status == Request.Connected) {
  ...
}
```

Where should we propose devs to define these consts that should be used at
call-sites? In nowadays practice, the namespace for them is a class object.
For now, they don't have other option than define them manually afterwards:

```
class Request {
  ...
}

Request.Initialized = 0;
Request.Connected = 1;
...
```

Potentially can be batch in to one call to `Object.mixin`, but the question
is: we got sugared classes now, why this desugared imperative class
constants definitions?

(the other option is to have a getter as was mentioned above, but this is
IMO to heavyweight for a simple constant)

Anyways, I just want to make sure, we don't make a design lack/mistake here
not providing this ability (and again, it's based not on simple design
thoughts (that are also true), but on actual practical questions from
programmers who already use ES6 classes).

>From this perspective, I'd still would ask for inclusion of this sugar:

```
class Request {
  const Connected = 1;
  ...
}
```

Into:

```
function Request() {}
Request.Connected = 1;
```

P.S. In this particular case, one could define two entities: the `Request`
class, and the `RequestStatus` object for storing the constants, but in
general case, classes still should have ability to serve as namespaces for
consts.

Dmitry


>>
>> 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/20131019/02ac015d/attachment.html>


More information about the es-discuss mailing list