Why use "with" keyword in JavaScript? [duplicate]

Possible Duplicate:
Are there legitimate uses for JavaScript’s “with” statement?

I recently discovered that in JavaScript, one can do something like the following:

with (document) {
    write('foo');
    body.scrollTop = x;
}

The down side of this is that each variable needs to be checked to see if it belongs to the document object, creating a significant overhead.

Alternatively, one could do something like this:

var d = document;
d.write('foo');
d.body.scrollTop = x;

Are there any situations where the use of the 'with' keyword is justified?


Solution 1:

Just don't use it: http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/

JavaScript's with statement was intended to provide a shorthand for writing recurring accesses to objects. So instead of writing

ooo.eee.oo.ah_ah.ting.tang.walla.walla.bing = true;
ooo.eee.oo.ah_ah.ting.tang.walla.walla.bang = true;

You can write

with (ooo.eee.oo.ah_ah.ting.tang.walla.walla) {
    bing = true;
    bang = true;
}

That looks a lot nicer. Except for one thing. There is no way that you can tell by looking at the code which bing and bang will get modifed. Will ooo.eee.oo.ah_ah.ting.tang.walla.walla be modified? Or will the global variables bing and bang get clobbered? It is impossible to know for sure...

If you can't read a program and be confident that you know what it is going to do, you can't have confidence that it is going to work correctly. For this reason, the with statement should be avoided...

Solution 2:

Despite advice to the contrary almost everywhere, I think that there are uses for "with". For example, I'm working on a domain model framework for Javascript, which uses the underscore character in much the same way that jQuery uses "$". This means that without "with", I have lots of underscores scattered through my code in ways that make it less readable. Here's a random line from an application using the framework:

_.People().sort(_.score(_.isa(_.Parent)),'Surname','Forename');

whereas with "with" it would look like

with (_) {
    ...

    People().sort(score(isa(Parent)),'Surname','Forename');

    ...
}

What would be really useful is a read-only version of "with".