javascript中Function与function的特殊之处 - 穷小子 - BlogJava - 已使用 Google 工具栏发送

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"

评论

此博客中的热门博文

你的妈妈已经等了二十几年

时间管理:每天挤出一小时

教您如何认识植柔皮,头层皮革,修面皮革,油蜡皮、水染皮、摔纹皮、纳帕皮、打蜡皮、压花皮、修面皮、漆光皮、磨砂皮、贴膜皮