两种方法,一种是加括号一种是加叹号,new Function()的方法没有成功。
var a = "function(args){console.log(args)}"
undefined
eval(eval('(' + a + ')')('test'))
test VM239:1
undefined
eval('!' + a)('test')
TypeError: boolean is not a function
eval('!' + a + "('test')")
test VM261:1
true
eval(eval('(' + a + ')' + "('test')"))
test VM263:1
undefined
var b = "not function"
undefined
eval(eval("'(' + b + ')')('test'))
SyntaxError: Unexpected token ILLEGAL
eval('!' + b + "('abc')")
SyntaxError: Unexpected token function
参考