0

The following example is one way in JavaScript to create and manipulate objects, i.e. with the new Object() syntax. The other way is by creating an object literal.

I remember reading somewhere but can't find it now that using "new Object()" to create objects in JavaScript should be avoided for some reason.

Is there a reason now to use new Object() as in the following code?

<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>
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • possible duplicate of [Is JavaScript 's "new" Keyword Considered Harmful?](http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful) – Darin Dimitrov Mar 04 '11 at 13:23
  • there is no good reason why you should not use new Object(), the only benefit is to use less code by using {}, thats all – Kris Ivanov Mar 04 '11 at 13:23
  • @KIvanov Less code is a *very* good reason actually :) – Ivo Wetzel Mar 04 '11 at 13:26
  • @Ivo Here, yes. In general, not necessarily. Don't run the risk of making your code completely impossible to read because you made it too esoteric with funny symbols and short, non-descrit identifiers. – Lightness Races in Orbit Mar 04 '11 at 13:34

4 Answers4

6

It's just a bit ugly. Write this instead:

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();
};

which does the same thing.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

No, there's no reason, but this is simpler (while equivalent):

var layout = {
  idCode   : 'simple',
  title    : 'Simple Layout',
  content  : '',
  width    : 400
  // etc.
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

I can't imagine this would cause any sort of problem, but if it bugs you, you could always:

var layout = {};

but there's no difference really.

spender
  • 117,338
  • 33
  • 229
  • 351
0

There is no difference besides to the fact that the above is horrible code and would never make it into the code base if I had to look over the commits.

11.1.5 Object Initialiser

Semantics
The production ObjectLiteral: { } is evaluated as follows:
1. Return a new object created as if by the expression new Object() where Object is the standard built- in constructor with that name.

See the spec for more boring details.

PS: new Object() might be a tiny bit faster, but that is completely dependent on the JS engine. And it's a FREAKING nano optimization.

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • 1
    -1 You didn't explain *why* you think it's horrible. Do you just think that it's ugly, or do you have other concerns? – Lightness Races in Orbit Mar 04 '11 at 13:32
  • Sorry, but I thought it was *freaking obvious* by now that literals and object notation are superior to the stuff in the OP in all possible ways. But, hey it's SO... shouldn't expect that much. – Ivo Wetzel Mar 04 '11 at 16:26
  • @Tomalak Geret'kal do you write `var temp = new String("")` or `var temp = ""`? `var temp = new Array(1)` or `var temp = []`, `var temp = new Number(0)` or `var temp = 0`. This stuff is obvouis. – Raynos Mar 04 '11 at 16:37
  • 2
    @IvoWetzel: Sure, it's obvious to you or me, but your answer is supposed to help the person asking the question, not people who already know. – Lightness Races in Orbit Mar 04 '11 at 17:25
  • @Raynos: I think you meant `new Array(0)`? – Lightness Races in Orbit Mar 04 '11 at 17:26
  • @Tomalak "The other way is by creating an object literal." The questioner knows that literals exist, he wanted to know whether there is any difference. So in that regard, your answer *completely* failed the question. – Ivo Wetzel Mar 04 '11 at 18:32
  • @IvoWetzel: Not true at all. It states that it "does the same thing", and always has done. – Lightness Races in Orbit Mar 04 '11 at 18:42
  • @TomalakGeret'kal why would you make an array with a length of 0? I would always default to 1. The former seems useless – Raynos Mar 05 '11 at 18:06
  • @Raynos: To make it empty before you add stuff to it? Otherwise you will have a useless initial meaningless value. Why would you want that? Anyway, my point was that `{}` is equivalent to `new Array(0)`, not `new Array(1)`. – Lightness Races in Orbit Mar 05 '11 at 18:27
  • @TomalakGeretKal You mean `[]` is equivelant to `new Array(0)`. I see your point. – Raynos Mar 05 '11 at 18:31