首页 > 技术文章 > js 执行字符串的函数

fanqiuzhuji 2020-12-21 22:08 原文

 


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)");


推荐阅读