5

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...
}
Joseph Shambrook
  • 320
  • 6
  • 14
  • 1
    `new` keyword is used to cause errors. At least now it will. Dunno is it was supported earlier. – Amit Joki Jan 14 '15 at 16:23
  • how does _with_ == ancient ? (new is likely a typo here) – dandavis Jan 14 '15 at 16:23
  • I wouldn't cite the `with` keyword as sign that code is old. While I don't generally recommend using it, there are some frameworks (namely frameworks that use templating such as KendoUI) that make good use of it. – Brian Ball Jan 14 '15 at 16:24
  • @dandavis no typos I'm afraid. Asking about the `new` keyword specifically, not `with`. – Joseph Shambrook Jan 14 '15 at 16:32
  • @BrianBall I definitely would. Been deprecated for 4 years, and recommended almost universally to not use it due to a security risk. Good use aside, it shouldn't be used at all. – Joseph Shambrook Jan 14 '15 at 16:33
  • 1
    but what are you asking about that use of _new_ though? someone didn't understand it once upon a time and misused it, and leter you came across it, what's the big deal? you haven't stumbled across some secret usage. – dandavis Jan 14 '15 at 16:35
  • a "security risk" by using _with_? this i gotta see. gotta link? – dandavis Jan 14 '15 at 16:35
  • 3
    @dandavis it makes behavior analysis really hard. Consider `with` used with a reference to an argument to a function. Also, `with` is disallowed in "strict" mode. – Pointy Jan 14 '15 at 16:39
  • 1
    @dandavis haha, [here you go](http://www.2ality.com/2011/06/with-statement.html). "with violates lexical scope, making program analysis (e.g. for security) hard to infeasible." My wording may have been a little extreme, but there is a "security" concern. – Joseph Shambrook Jan 14 '15 at 16:42
  • @dandavis well that's exactly what I was wondering. I can't get into the minds of the previous developers, and couldn't think (or find) a reason for it! – Joseph Shambrook Jan 14 '15 at 16:45
  • i am aware that ES3CP/ES5.strict don't support it, and of it's potential gotchas, but to say that it's a security risk is simply misleading. how many people use "program analysis" to validate security? i've never seen that done or recommended, ever, except with ports like FBJS. in short, _with_ has enough strikes against it that we need not overstate theoretical reasons not to implement it... besides, its _eval()_ that's the infamous security risk... how about we all just not use _new_ or _with_ ? – dandavis Jan 14 '15 at 16:49
  • @dandavis rather unfortunate that this turned into a small discussion about something unrelated to my actual question... – Joseph Shambrook Jan 15 '15 at 17:16
  • you asked why someone sometime did something useless, but nobody knows why, besides whomever wrote it. The other parts of your question were/are more easily addressed. in short, i don't think there's really a good answer to be had. Sorry if i wasted time drilling to that conclusion. – dandavis Jan 15 '15 at 18:12

1 Answers1

1

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.

Community
  • 1
  • 1
Joseph Shambrook
  • 320
  • 6
  • 14
  • It's a TypeError, not a syntax error. The difference is that if your code contains a syntax error, none of it will run, whereas a TypeError only happens when the line of code containing the error runs (and maybe it never will). – Jason Orendorff Jun 04 '15 at 11:12