Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...
}
like image 313
Joseph Shambrook Avatar asked Nov 10 '22 20:11

Joseph Shambrook


1 Answers

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.

like image 69
Joseph Shambrook Avatar answered Nov 15 '22 12:11

Joseph Shambrook