Defining Computed Members
Sometimes we like to give dynamic names to members of a class—that is, to fields, properties, and methods. The approach to achieve this is also useful as a workaround to provide arbitrary names for members, bypassing the language’s restriction on making the members with only alphanumeric characters.
Recall using special method names like Symbol.search and Symbol.iterator in Chapter 4, Iterators and Symbols. Writing a method named Symbol.iterator() {} will result in an unexpected token error. We had to place it within [], like so: [Symbol.iterator]() {}.
To create a computed property, we place a variable containing the name of the property as the value in []. Alternatively, we can place a string or a template literal ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access