zoukankan      html  css  js  c++  java
  • ☀【函数】new Function('') / new function(){}

    <!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>

    《JavaScript权威指南》

    详解new function(){}和function(){}()
    http://www.planabc.net/2008/02/20/javascript_new_function/

  • 相关阅读:
    HTML5学习小结
    HTML和CSS的复习总结
    LOL UVALive
    E
    D
    C
    B
    D
    J
    css
  • 原文地址:https://www.cnblogs.com/jzm17173/p/2698197.html
Copyright © 2011-2022 走看看