eval和new Function 邪恶的eval和new Function使用介绍

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

eval和new Function 邪恶的eval和new Function使用介绍

  2021-03-19 我要评论
想了解邪恶的eval和new Function使用介绍的相关内容吗,在本文为您仔细讲解eval和new Function的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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);'))();


解释:
1、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.

2、new Function(argument_names..., body): 注意参数中的body. 这样,用new Function('body')()
, 也可以像eval一样动态执行代码。

3、array.join(separator): 这个最简单,不多说。在这里,巧妙的用来解决了一个无聊问题:写段代码,运行后打印出从1到100的整数,不允许使用循环、跳转和递归。

这些代码可以干什么?可以肯定的是可以干很多猥琐的事。具体是啥呢,自己想啰。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们