穷小子

MajorYe
数据加载中……
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 阅读(411) 评论(0)  编辑  收藏 所属分类: WEB开发


只有注册用户登录后才能发表评论。


网站导航: