How to execute a JavaScript function when I have its name as a string?

JavascriptWeb DevelopmentFront End Technology

To execute a JavaScript function when its name is as a string, you can access class methods by name

Example

Live Demo

<html>
   <body>
      <script>
         class Demo {
            methodOne(){
               document.write("50");
            }
            methodTwo(){
                this['methodOne']();
                document.write("<br> 100");
            }
         }
         let num = new Demo();
         num['methodTwo']();
      </script>
   </body>
</html>
raja
Published on 29-Jan-2018 15:17:48
Advertisements