javascript中Function與function的特殊之處
Function()的特殊之處有三點:
1: Function() constructor 它是允許js引擎動態的去編譯和運行,所以它很像全局的eval()。
【注】:可別小看這個eval(),它可是js的一個解釋器哦,嘿嘿!
2:正因為 Function() constructor 是動態的去創建函數體,因此它會比直接function定義函數要消耗資源,
特別在循環中不推薦使用
3:這點也是 Function() constructor 最重要的一點, Function() constructor 創建的函數是全局的,而不是相
應得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) 評論(0) 編輯 收藏 所屬分類: WEB開發