Joseph Shambrook
Joseph Shambrook

Reputation: 321

JavaScript: Ancient code starts simple object using 'new' keyword. Why?

I guess I'm still relatively new to JS development, and during some refactoring of ancient JS code (proof: there's still usage of the 'with' statement in there), I've come across the following:

var result = new {
    key: 'value'
    // etc...
}

Why is the new keyword used? Is there a difference between this and the following?

var result = {
    key: 'value'
    // etc...
}

Upvotes: 5

Views: 98

Answers (1)

Joseph Shambrook
Joseph Shambrook

Reputation: 321

After wasted time of researching this and waiting to see if anyone had any clue what these previous devs were doing, I've decided to answer it myself.

From a separate Stack question, located here, this seemed a little relevant:

It creates a new object. The type of this object, is simply object.

So whether it worked in an old browser or whatever, it appears that this snippet was a disjointed way of creating a new object. Modern browsers (Chrome) throw syntax errors upon encountering this, so if it ever was valid, it isn't now.

Upvotes: 1

Related Questions