字符串转js脚本执行
1.eval不推荐
var a = 'global scope';
function b(){
var a = 'local scope'
eval('console.log(a)'); //local scope
}
b();
2.new Function()
new Function(arg1, arg2, ..., argN, function_body);中的参数和函数体都以字符串形式传入。
var a = 'global scope';
function b(){
var a = 'local scope';
(new Function('','console.log(a)'))(); //global scope
}
b();
let a=1
let b=(param)=>{
return param
}
const func='return arg1+arg2(2)'
const Fn = new Function('arg1','arg2',func) //arg1对应下面Fn(a ,b)中参数a
Fn(a ,b)