-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support computed properties in class property declarations #1841
Comments
We would only do this for initialized properties. For uninitialized properties, we would have to give an error. |
So if you had something like var x = 10;
class C {
[x++] = x;
} I'm assuming that every time the constructor is invoked, the |
No, that is not the semantics I had in mind. It would get evaluated when the class is evaluated, which happens only once. |
Why? We don't cache the initializers anyway. TypeScript input: var x = 0;
function f(): number {
return x++;
}
class Greeter {
id: number = f();
constructor() {
}
} JavaScript output: var x = 0;
function f() {
return x++;
}
var Greeter = (function () {
function Greeter() {
this.id = f();
}
return Greeter;
})(); |
Yeah but we would cache the property name. It feels different from the initializer. But I see what you're saying, we could move the name into the constructor too, unless anyone has objections to that. |
As per our design meeting last week, we've decided that we just won't support this because the semantic implications can be counterintuitive. |
We can emit computed properties class properties, using the following scheme:
emits as:
The text was updated successfully, but these errors were encountered: