A legitimate use for "with" statement

Non-programmers: This is esoteric. You can safely skip this post.

Now, as a programmer, I hate the with statement. I never use them. Back when I did they were the source of many ridiculous bugs. Saving a few characters in the source while obscuring the meaning of the code. Not a good tradeoff.

However, there is a legitimate use for a with statement, and it's disturbing to see that it's been deprecated in JavaScript. I hope this does not mean that there will ever be a version of JavaScript that doesn't have it.

Here's when you need it: When you're implementing a macro language.

  • with (context) {eval (macro)}

It's the same use-case as the much-maligned eval function.

  • eval used to have a second param, btw, that allowed the caller to provide a context that the code runs in. They took this off, but I wish they hadn't. (Scroll to the bottom of this Mozilla tech note for the historical note.)

  • We added these to Frontier, and were able to do wonderful stuff, like XML-RPC for example.

Imho, eval and with should only be used by people implementing macro environments.

But they are useful. And both with and eval should exist.


Last built: Thu, Apr 17, 2014 at 10:38 PM

By Dave Winer, Thursday, December 12, 2013 at 2:42 PM. All baking done on premises.