If the "with" statement in Javascript creates a new scope, why does the following code not work as e
- by Jian Lin
If the "with" statement in Javascript creates a new scope, shouldn't clicking on the links show a different x which are in different scopes? It doesn't.
<a href="#" id="link1">ha link 1</a>
<a href="#" id="link2">ha link 2</a>
<a href="#" id="link3">ha link 3</a>
<a href="#" id="link4">ha link 4</a>
<a href="#" id="link5">ha link 5</a>
<script type="text/javascript">
for (i = 1; i <= 5; i++) {
with({foo:"bar"}) {
var x = i;
document.getElementById('link' + i).onclick = function() { alert(x); return false; }
}
}
</script>