js 执行字符串的函数

 


1.使用new Function  点击查看文档
var func = 'function test(_this,res){console.log(res,"===",_this)}';
var _this = "_this";
var res = {name:'***',age:'21',sex:1,list:[1,2,3]}
function Func(func,res){
     var dom = new Function(`return ${func}`)();
    console.log(dom,'dom');
    dom && dom(_this,res);
}
 Func(func,res);



2.使用eval  点击查看文档
eval("console.log(1111111111)");


posted @ 2020-12-21 22:08  自律·给我自由  阅读(1802)  评论(0编辑  收藏  举报