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...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With