| <p>This page tests var declarations inside various block structures and control statements.</pre> |
| <pre id="console"></pre> |
| |
| <script> |
| if (window.testRunner) |
| testRunner.dumpAsText(); |
| |
| function log(s) |
| { |
| document.getElementById("console").appendChild(document.createTextNode(s + "\n")); |
| } |
| |
| function shouldBe(a, b) |
| { |
| var evalA; |
| try { |
| evalA = eval(a); |
| } catch (e) { |
| evalA = e; |
| } |
| |
| if (evalA === b) { |
| log("PASS: " + a + " should be " + b + " and is."); |
| } else { |
| log("FAIL: " + a + " should be " + b + " but instead is " + evalA + "."); |
| } |
| } |
| |
| function testDeclarations(title) |
| { |
| var vars = [ |
| "v1", |
| "v2", |
| "v3", |
| "v4", |
| "v5", |
| "v6", |
| "v7", |
| "v8", |
| "v9" |
| ]; |
| |
| log(title); |
| log("-----"); |
| for (var i = 0; i < vars.length; ++i) |
| shouldBe("'" + vars[i] + "' in window", true); |
| } |
| |
| testDeclarations("Before executing blocks containing var declarations: "); |
| |
| { |
| var v1; |
| } |
| |
| if (false) { |
| var v2; |
| } |
| |
| switch (true) { |
| case true: { |
| var v3; |
| break; |
| } |
| case false: { |
| var v4; |
| break; |
| } |
| } |
| |
| for (var i = 0; i < 0; ++i) { |
| var v5; |
| } |
| |
| do { |
| var v6; |
| } while(0); |
| |
| while (0) { |
| var v7; |
| } |
| |
| for (var p in {}) { |
| var v8; |
| } |
| |
| with ({}) { |
| var v9; |
| } |
| |
| log(""); |
| testDeclarations("After executing blocks containing var declarations: "); |
| |
| </script> |