Propose simpler string constant

Brendan Eich brendan.eich at gmail.com
Wed Dec 16 18:04:23 UTC 2015


On Wed, Dec 16, 2015 at 9:41 AM Alican Çubukçuoğlu <
alicancubukcuoglu at gmail.com> wrote:

> How are enums declared?
> ```javascript
> let myVar = 13;
> enum myEnum = {Red, Green, Blue};
>

No `=` between name and `{`.

Enumerator scope is a good question. Clearly we do not want global scope.
Rather, as a declaration immedicately contained by a block or top level, we
want lexical scope for the enum name -- and (I think) for the enumerators'
individual names.

What about enumerator name scope for enum in class, without `static`? I'm
not sure, but precedent says that the enumerator names define prototype
properties.

Expression as well as declaration `enum` form follows class and function
precedent. Expression form requires a reserved identifier (not sym or sum
or whatever), which `enum` has been forever, fortunately.

I agree symbol values by default, with ` = 0` or some other number than 0
after the first enumerator name, looks confusing. Recall the first use-case
in the o.p. was (implicit, should rather be explicit) reflection on the
string value that spells the enumerator name.

/be


>
> // How does it work with classes?
> class MyClass {
>   static myVar = 13; // "Class Fields & Static Properties" which omits
> "let"
>   static enum myEnum = {Red, Green, Blue}; // Now we decided to use "enum"
> while we omitted "let" before
>   // Maybe "Class Fields & Static Properties" should use "let" and "const"?
> }
> ```
>
> Or:
> ```javascript
> let myVar = 13;
> let myEnum = enum {Red, Green, Blue};
>
> // How does it work with classes?
> class MyClass {
>   static myVar = 13; // "Class Fields & Static Properties" which omits
> "let"
>   static myEnum = enum {Red, Green, Blue}; // All fine?
> }
> ```
>
> ---
>
> Do we allow this:
> ```javascript
> let myEnum = enum {Red, Green, Blue};
> myEnum.Red; // 0 or Symbol("Red")
> myEnum[myEnum.Red]; // Can we get "Red" like this?
> ```
>
> If we do, how does this work?
> ```javascript
> let myEnum = enum {Red, Green, Blue};
> myEnum.Alpha = 3;
> myEnum[myEnum.Alpha]; // Who is going to put "Alpha" here? It magically
> happens? Probably not.
> ```
>
> ---
>
> Take `HTMLMediaElement` for an example, which has these:
> - HTMLMediaElement.HAVE_NOTHING  = 0
> - HTMLMediaElement.HAVE_METADATA = 1
> - HTMLMediaElement.HAVE_CURRENT_DATA = 2
> - HTMLMediaElement.HAVE_FUTURE_DATA = 3
> - HTMLMediaElement.HAVE_ENOUGH_DATA = 4
>
> And they can be used like this:
> ```javascript
> if (myVideo.readyState >= HTMLMediaElement.HAVE_METADATA) {
>   goodToGo();
> } else {
>   waitForIt();
> }
> ```
>
> How are we going to achieve the same functionality with "Symbol Enums"?
> Don't use Symbols?
> Make Symbols comparable or use something special like NumberSymbol which
> is comparable like this?
> Don't allow users to do this and have them implement methods like
> `isReadyStateGreaterThan()` or `readyStateToNumber()`?
> ???
>
> AFAIK, enums can do this in other languages:
> ```javascript
> let myEnum = enum {Red = 2, Green, Blue};
> // Green is 3, Blue is 4.
> ```
>
> Maybe we can give the user to pick one:
> ```javascript
> let myEnum = enum {Red, Green, Blue}; // Symbol Enum
> let myEnum = enum {Red = 0, Green, Blue}; // Number Enum
> ```
>
> Looks confusing.
>
> ---
>
> How will a user be able to achieve an `HTMLMediaElement` like result?
> (Making enum'd values properites of the class)
> ```javascript
> class HTMLMediaElement extends HTMLElement {
>   // Like this?
>   static enum {HAVE_NOTHING, HAVE_METADATA/*, ...*/}
>
>   // A "Number Enum" generates an object so use it with something crazy
> like "Class Spread Properties"?
>   static ...enum {HAVE_NOTHING, HAVE_METADATA/*, ...*/} // What?
>
>   // Maybe not supporting anything like above, and forcing users to put
> enums in a property
>   static readyStates = enum {HAVE_NOTHING, HAVE_METADATA/*, ...*/};
> }
> ```
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151216/08717e9b/attachment.html>


More information about the es-discuss mailing list