eval和new Function使用介紹
更新時(shí)間:2012年12月27日 15:16:41 投稿:mdxy-dxy
這些代碼可以干什么?可以肯定的是可以干很多的事
代碼:
復(fù)制代碼 代碼如下:
// 友善提醒:為了你的手指安全,請(qǐng)?jiān)贑hrome下運(yùn)行
'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一樣動(dòng)態(tài)執(zhí)行代碼。
3、array.join(separator): 這個(gè)最簡單,不多說。在這里,巧妙的用來解決了一個(gè)無聊問題:寫段代碼,運(yùn)行后打印出從1到100的整數(shù),不允許使用循環(huán)、跳轉(zhuǎn)和遞歸。
這些代碼可以干什么?可以肯定的是可以干很多的事。具體是啥呢,自己想啰。
相關(guān)文章
通過vbs修改目錄名稱與快捷方式名稱的實(shí)現(xiàn)代碼
這篇文章主要是分享了如果用vbs修改目錄名與快捷方式名稱的代碼,需要的朋友可以參考下2013-07-07VBS基礎(chǔ)篇 - vbscript Dictionary對(duì)象
Dictionary是存儲(chǔ)數(shù)據(jù)鍵和項(xiàng)目對(duì)的對(duì)象,其主要屬性有Count、Item、Key,主要方法有Add、Exists、Items、Keys、Remove、RemoveAll2018-05-05顯示運(yùn)行對(duì)話框內(nèi)保存的命令歷史的vbs
這段代碼可以獲取曾經(jīng)在運(yùn)行對(duì)話框中的,我們之前輸入的命令歷史2008-10-10