eval和new Function使用介紹
更新時間:2012年12月27日 15:16:41 投稿:mdxy-dxy
這些代碼可以干什么?可以肯定的是可以干很多的事
代碼:
復制代碼 代碼如下:
// 友善提醒:為了你的手指安全,請在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): 注意參數(shù)中的body. 這樣,用new Function('body')()
, 也可以像eval一樣動態(tài)執(zhí)行代碼。
3、array.join(separator): 這個最簡單,不多說。在這里,巧妙的用來解決了一個無聊問題:寫段代碼,運行后打印出從1到100的整數(shù),不允許使用循環(huán)、跳轉(zhuǎn)和遞歸。
這些代碼可以干什么?可以肯定的是可以干很多的事。具體是啥呢,自己想啰。
相關(guān)文章
VBS基礎篇 - vbscript Dictionary對象
Dictionary是存儲數(shù)據(jù)鍵和項目對的對象,其主要屬性有Count、Item、Key,主要方法有Add、Exists、Items、Keys、Remove、RemoveAll2018-05-05