pass function in json and execute

前端 未结 7 1913
夕颜
夕颜 2020-11-28 07:15

Is there any way that I can pass a function as a json string (conversion with JSON.stringify), send it to another function, parse the json and then execute the function that

7条回答
  •  时光取名叫无心
    2020-11-28 08:07

    Yes, you can. There are tons of ways to do it.

    And there is no need to use the "evil" eval function (please yahoogle why it should be avoided) as pointed out here: http://javascript.about.com/library/bleval.htm

    var tmpFunc = new Function(codeToRun);
    tmpFunc(); 
    

    Whether it was JSON at any stage should be irrelevant.

提交回复
热议问题