Why use "with" keyword in JavaScript?

Javascript

Javascript Problem Overview


> 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?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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".

Solution 3 - Javascript

I would avoid using it in production code because it's ambiguous but there is an alternative solution to the for-loop-closure solution by using with to mimic the let binding, here's a copy of my previous answer:

An alternative to the standard closure solution using functions inside of a for loop:

<a  href="#">blah</a><br>
<a  href="#">blah</a><br>
<a  href="#">foo</a><br>
<script>
    (function() {
	var anchors = document.getElementsByTagName('a');
	    for ( var i = anchors.length; i--; ) {
	        var link = anchors[i];
	        with ({ number: i }) {
		        link.onclick = function() {
	   	            alert(number);
		        };
	        }
	    }
    })();
</script>

Credit to nlogax for providing a solution which I pretty much ripped off: https://stackoverflow.com/questions/1451009/javascript-infamous-loop-problem

Here's the standard solution:

<script>
    (function() {
	var anchors = document.getElementsByTagName('a');
	for ( var i = anchors.length; i--; ) {
	    var link = anchors[i];
	    (function(i) {
		    link.onclick = function() {
		        alert(i)
		    }
	    })(i);
	}
    })();
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn McCollumView Question on Stackoverflow
Solution 1 - JavascriptazazulView Answer on Stackoverflow
Solution 2 - JavascriptRichView Answer on Stackoverflow
Solution 3 - Javascriptmeder omuralievView Answer on Stackoverflow