just dont use it : http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/
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:
http://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>
Here are some blog posts in support of the with keyword. But please read the YUI blog entry that azazul posted as well!
http://webreflection.blogspot.com/2009/12/with-worlds-most-misunderstood.html http://webreflection.blogspot.com/2009/12/with-some-good-example.html
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".