Given a string describing a Javascript function, convert it to a Javascript function

前端 未结 8 1047
广开言路
广开言路 2020-11-27 04:38

Say I\'ve got a Javascript string like the following

var fnStr = \"function(){blah1;blah2;blah3; }\" ;

(This may be from an expression the

8条回答
  •  执笔经年
    2020-11-27 04:56

    You can call Parse your string as javascript fuction

    1. function getDate(){alert('done')} // suppose this is your defined function

    to call above function getDate() is this in string format like 'getDate()'

    1. var callFunc=new Function('getDate()') //Parse and register your function

    2. callFunc() // Call the function

提交回复
热议问题