邪恶的eval和new Function
代码:
// 友善提醒:为了你的手指安全,请在Chrome下运行
'alert("hello")'.replace(/.+/, eval);
'alert("hello")'.replace(/.+/, function(m){new Function(m)();});
var i = 0; eval(new Array(101).join('alert(++i);'));
var i = 0; new Function(new Array(101).join('alert(++i);'))();
解释:
string.replace(regexp, replacement): replacement可以是function. In this case, the function is invoked for each match, and the string it returns is used as the replacement text.new Function(argument_names..., body): 注意参数中的body. 这样,用new Function('body')(), 也可以像eval一样动态执行代码。array.join(separator): 这个最简单,不多说。在这里,巧妙的用来解决了一个无聊问题:写段代码,运行后打印出从1到100的整数,不允许使用循环、跳转和递归。
这些代码可以干什么?可以肯定的是可以干很多猥琐的事。具体是啥呢,自己想啰。

February 11th, 2009 on 9:34
邪恶…….
February 11th, 2009 on 10:15
想不出来……
February 11th, 2009 on 21:43
貌似比较强。。。
eval(['var i=sum=0;',new Array(100).join('sum+=++i;'),'alert(sum);'].join(”))
February 11th, 2009 on 22:06
没装Chrome,我的手指受伤了。
March 4th, 2009 on 20:25
非常不错误!
March 5th, 2009 on 21:28
new Array(101).toString().replace(/,/g, function(m, i) {print(++i)});
比较直接
August 12th, 2009 on 21:51
new Function(‘a’,new Array(10).join(‘alert(++a);’))(0);
leave a reply