Computed method names

You can also decide the name of static and non-static methods of a class and concise methods of an object literal at run-time; that is, you can define method name via expressions. Here is an example to demonstrate this:

class myClass {     static ["my" + "Method"]() {         console.log("Hello");     } } myClass["my" + "Method"](); //Output "Hello"

Computed property names also allow you to use symbols as keys for the methods. Here is an example to demonstrate this:

var s = Symbol("Sample"); class myClass {     static [s]() {         console.log("Hello");     } } myClass[s](); //Output "Hello"

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.