Tobiq
Tobiq

Reputation: 2657

Set property of a class using a variable

Is something like this possible?

const propName = "x";

class A {
    static propName = 1
    // equivalent to static x = 1
}

A[propName] // evaluates to 1

or would it be (typeof A)[propName]?


For obvious reasons, this is not a duplicate of this question

Upvotes: 1

Views: 58

Answers (1)

Tobiq
Tobiq

Reputation: 2657

This is possible, simply as:

const propName = "x";

class A {
    static [propName] = 1
    // equivalent to static x = 1
}

A[propName]

Upvotes: 2

Related Questions