Convert a string to a function, gives ReferenceError

后端 未结 1 802
星月不相逢
星月不相逢 2021-01-28 01:27

I need to call a function but it is given as a string (originally it is much more complex and includes many more nested m() functions.):

\"m(\'p\',          


        
相关标签:
1条回答
  • 2021-01-28 01:41

    In order to use the function m in that code string, you need to pass it as a parameter to the function:

    const m = require(...);
    const f = new Function("m", "m('p', {class:'red'}, 'text')");
    f(m);
    
    0 讨论(0)
提交回复
热议问题