jzm17173 发表于 2013-1-4 02:42:52

【函数】♣new Function('')和new function(){}

<div id="cnblogs_post_body"><div class="cnblogs_code"><!DOCTYPE html><html lang="zh-CN"><head>    <meta charset="utf-8" />    <title></title></head><body>    <script>      var f = new Function('x', 'y', 'return x + y;');// 等价于var f = function(x, y){return x + y;}      console.log(f(1, 1));      var str = '{"id": 108}';      console.log((new Function('return ' + str))());// 字符串转对象      f = new function() {return 'ca';};      console.log(f);      // 相当于      /*function 匿名类() {            return 'ca';      }      f = new 匿名类();      console.log(f);*/      f = new function() {return new String('ca');};      console.log(f);      // 只要 new 表达式之后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象,如果返回(return)一个原始类型(无 return 时其实为 return 原始类型 undefined),那么就返回 new 创建的匿名对象    </script></body></html>
页: [1]
查看完整版本: 【函数】♣new Function('')和new function(){}