Sometimes, we want to use dynamic function name in JavaScript.
In this article, we’ll look at how to use dynamic function name in JavaScript.
How to use dynamic function name in JavaScript?
To use dynamic function name in JavaScript, we can create an object with the function name.
For instance, we write
const name = "myFn";
const fn = { [name]() {} }[name];
to set the fn
variable to a function with the name name
.
We put the name
method in the object and then we get the method with [name]
.
Conclusion
To use dynamic function name in JavaScript, we can create an object with the function name.