最近项目中使用百度模板引擎baiduTemplate.js,使用动态函数解析模板中代码。

通过new Function([arg1,arg2,...,argN,]functionBody)方式实现动态函数调用。

如下:

<!doctype html>
<html>
 <head>
  <script type="text/javascript">
    var funBody = "var sum=0;for(var i=1;i<=10;i++){sum+=i;};alert(sum + x + y);";
    var a = 1,b=2;
    var newFun = new Function("x","y",funBody);
    newFun(a,b);
  </script>
 </head>
 <body>
 </body>
</html>

 

相关文章:

  • 2021-11-18
  • 2022-02-01
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2021-08-03
  • 2022-12-23
  • 2022-02-08
  • 2021-07-27
  • 2021-11-30
相关资源
相似解决方案