Closed michaelficarra closed 8 years ago
We need to retain computed property names for purposes of things like symbol-named properties (at least):
var stuff = Symbol("stuff");
class Foo {
[stuff] = 42;
}
On the syntax looking like an assignment front: I think this confusion has come up in a few other places and as a result I've been toying with a few options. Perhaps a special field-initializer operator such as =:
or maybe even <-
would help make it clear that these initializations are not assignments in the normal sense.
=:
could get a little funky looking with type annotations:
class Foo {
foo:number =: 42
}
<-
is pretty pleasant, though:
class Foo {
foo:number <- 42
}
I initially was thinking :
for consistency with object properties, but that would get in the way of your type annotations if both are optional. Sticking with =
is fine, since it is consistent with destructuring/parameter defaults, an even more related concept. Still, I wish there was a way to support the symbol use case without allowing arbitrary expressions. Please ensure the semantics are defined in a way that evaluates the property name only once.
You certainly would not want the name to be evaluated and potentially different for each instance. If the name is only evaluated once, that is a strange inconsistency between how the left and right expressions are evaluated. Additionally, properties like
[a] = [0]
really look like destructuring bindings, which would be confusing for readers.