有没有理由不在JavasScript中使用`new Object()`?

Edw*_*uay 0 javascript

以下示例是JavaScript中用于创建和操作对象的一种方式,即使用new Object()语法.另一种方法是创建一个对象文字.

我记得在某个地方阅读但现在无法找到它因为某种原因应该避免使用"new Object()"在JavaScript中创建对象.

是否有理由现在使用new Object()如下代码?

<html>
    <head>
        <title>Test Page</title>
        <script type="text/javascript">
            window.onload = function() {

                var layout = new Object();
                layout.idCode = 'simple';
                layout.title = 'Simple Layout';
                layout.content = '';
                layout.width = 400;
                layout.display = function() {
                    return '<div style="background-color: #eee; width:'+this.width+'">'+this.content+'</div>'
                };

                layout.width = 200;
                layout.content = 'This is the new content';

                document.getElementById('output').innerHTML = layout.display();
            };
        </script>
    </head>
    <body>
        <div id="output"></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Lig*_*ica 6

这有点难看.写这个:

window.onload = function() {

    var layout = {
        idCode:   'simple',
        title:    'Simple Layout',
        content:  '',
        width:    400,
        display:  function() {
            return '<div style="background-color: #eee; width:'+this.width+'">'+this.content+'</div>'
        },

        width:    200,
        content:  'This is the new content'
    };
    document.getElementById('output').innerHTML = layout.display();
};
Run Code Online (Sandbox Code Playgroud)

它做同样的事情.