How invoke private function by name

后端 未结 1 1250
刺人心
刺人心 2021-01-26 08:11

How invoke function by name? Actually I know how invoke function by name, but I can\'t find needed scope. So now I have to use this[\"get\" + widgetName]

1条回答
  •  面向向阳花
    2021-01-26 08:37

    You can't invoke private functions via a string variable except via eval(), and that's generally a bad idea.

    However you can trivially create a private object that contains references to those private methods, and then index into that to get the desired function:

    function widgetGeneratorService(...) {
    
        var methods = {
            func1: func1
            ...
        };
    
        methods['func1']();
    
    }
    

    0 讨论(0)
提交回复
热议问题