js 执行一个字符串类型的函数

两种方法,一种是加括号一种是加叹号,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

参考

http://zhidao.baidu.com/question/504132293.html

http://blog.csdn.net/zhaozhi406/article/details/9672481

posted @ 2014-07-29 11:46  nil  阅读(575)  评论(0编辑  收藏  举报