javascript中Function與function的特殊之處
Function()的特殊之處有三點(diǎn):
1: Function() constructor 它是允許js引擎動(dòng)態(tài)的去編譯和運(yùn)行,所以它很像全局的eval()。
【注】:可別小看這個(gè)eval(),它可是js的一個(gè)解釋器哦,嘿嘿!
2:正因?yàn)?nbsp;Function() constructor 是動(dòng)態(tài)的去創(chuàng)建函數(shù)體,因此它會(huì)比直接function定義函數(shù)要消耗資源,
特別在循環(huán)中不推薦使用
3:這點(diǎn)也是 Function() constructor 最重要的一點(diǎn), Function() constructor 創(chuàng)建的函數(shù)是全局的,而不是相
應(yīng)得scope里面的
eg:
var y = "global";
function constructFunction() {
var y = "local";
return new Function("return y"); // Does not capture the local scope!
}
// This line displays "global" because the function returned by the
// Function() constructor does not use the local scope. Had a function
// literal been used instead, this line would have displayed "local".
alert(constructFunction()()); // Displays "global"
posted on 2008-04-17 18:03 MajorYe 閱讀(426) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): WEB開(kāi)發(fā)