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?

3
  • 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, 2012 at 21:05
  • 3
    This question did not deserve a down-vote
    – Ben
    Mar 25, 2012 at 21:07
  • 2
    BTW HTML 4 or 5 is irrelevant. Javascript/ECMAScript is not part of HTML.
    – Colin Fine
    Mar 25, 2012 at 21:21

3 Answers 3

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

4
  • What the... o.O What do a = [b][b = a,0]; do? Mar 25, 2012 at 21:14
  • I don't get it... [b][b = a,0] wtf...is that? Mar 25, 2012 at 21:18
  • 1
    @Derek. Check the updated fiddle. It swaps a and b. Now do you understand why am I right...? hacks are cool, but not in a real code!
    – gdoron
    Mar 25, 2012 at 21:18
  • Very confusing, and now I really curious that how [b][b = a,0] works. :P Mar 25, 2012 at 21:30
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.

6
  • 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, 2012 at 21:06
  • @Quentin. Funny, I just added that kind of remark to my answer. lol
    – gdoron
    Mar 25, 2012 at 21:07
  • But why didn't they recommend with()? Mar 25, 2012 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. Mar 25, 2012 at 21:10
  • 3
    It's also forbidden to use in strict mode (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, 2012 at 21:11
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.