4

Once, I saw an example like this:

var a, x, y;
var r = 10;
with (Math) {
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
}

And it looks very convenience, because that way I don't have to type all the Math.s.

But when I take a look at the MDN, it says:

Using with is not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.

So is it okay to use with()? In HTML5?

gdoron is supporting Monica
  • 136,782
  • 49
  • 273
  • 342
Derek 朕會功夫
  • 84,678
  • 41
  • 166
  • 228
  • Define "okay". If you mean "recommended" then no (at least by one good source), and you've quoted the evidence. If you mean "will it work", then maybe, it depends if you are using strict mode or not. – Quentin Mar 25 '12 at 21:05
  • 3
    This question did not deserve a down-vote – Ben Mar 25 '12 at 21:07
  • 2
    BTW HTML 4 or 5 is irrelevant. Javascript/ECMAScript is not part of HTML. – Colin Fine Mar 25 '12 at 21:21

3 Answers3

4

The MDN you linked says Using with is not recommended...
with is an excellent way of making spaghetti code for lunch.

You might like it, but the guy that will need to debug it will curse you.

javascript has some very weird operators, like the comma operator(,).
Can you understand what the following code does?

var a = "a";
var b = "b";
a = [b][b = a,0];

Well it swaps a and b... You don't understand , so as the guy that will need maintain your with code. Don't use hacks, hacks are cool in charades games, not in real code.


When is the comma operator useful?
The comma swap Fiddle

Community
  • 1
  • 1
gdoron is supporting Monica
  • 136,782
  • 49
  • 273
  • 342
3

It is okay to use any feature of JavaScript, so long as you understand it.

For example, using with you can access existing properties of an object, but you cannot create new ones.

Observe:

var obj = {a:1,b:2};
with(obj) {
    a = 3;
    c = 5;
}
// obj is now {a:3,b:2}, and there is a global variable c with the value 5

It can be useful for shortening code, such as:

with(elem.parentNode.children[elem.parentNode.children.length-3].lastChild.style) {
    backgroundColor = "red";
    color = "white";
    fontWeight = "bold";
}

Because the properties of the style object already exist.

I hope this explanation is clear enough.

Niet the Dark Absol
  • 301,028
  • 70
  • 427
  • 540
  • 4
    "It is okay to use any feature of JavaScript, so long as you understand it." … and are the only person who will ever maintain it. – Quentin Mar 25 '12 at 21:06
  • @Quentin. Funny, I just added that kind of remark to my answer. **lol** – gdoron is supporting Monica Mar 25 '12 at 21:07
  • But why didn't they recommend `with()`? – Derek 朕會功夫 Mar 25 '12 at 21:08
  • Just because I say it's okay to use, doesn't mean I use it :p I like abbreviating with: `a = some.long.object.reference.here` and just assigning to `a`'s properties. – Niet the Dark Absol Mar 25 '12 at 21:10
  • 3
    It's also forbidden to use in strict mode (https://developer.mozilla.org/en/JavaScript/Strict_mode) which is a good indicator that they'll want it out of the language altogether in the future – Ben Mar 25 '12 at 21:11
  • No. Crockford shows that "so long as you understand" isn't enough; the code can have a different meaning from what you intend. If you are unfortunate enough, in your example example above, to have variables called "backgroundColor", "color" and "fontWeight", then if the expression in the parenthesis happens to evaluate to undef, the code will succeed and assign to those variables. – Colin Fine Mar 25 '12 at 21:17
2

In his excellent book "Javascript: The Good Parts", Douglas Crockford lists the "with Statement" in Appendix B: The Bad Parts.

He says "Unfortunately its results can sometimes be unpredictable, so it should be avoided".

He goes on to give an example, where an assignment inside the with will operate on different variables depending on whether the object is defined or not.

See With statement considered harmful (but less detailed than the explanation in the book).

Colin Fine
  • 3,323
  • 1
  • 18
  • 30