Skip to content

Instantly share code, notes, and snippets.

@grauwoelfchen
Last active December 21, 2015 09:19
Show Gist options
  • Save grauwoelfchen/6284583 to your computer and use it in GitHub Desktop.
Save grauwoelfchen/6284583 to your computer and use it in GitHub Desktop.
Test of JavaScript Scope
// test
var testScope = function(outer, inner, a, b, c) {
test("", function() {
assert(outer === "function", "outer() is in scope");
});
test("", function() {
assert(inner === "function", "inner() is in scope");
});
test("", function() {
assert(a === "number", "a is in scope");
});
test("", function() {
assert(b === "number", "b is in scope");
});
test("", function() {
assert(c === "number", "c is in scope");
});
};
window.onload = function() {
test("--- before outer ---", function() {
testScope(typeof outer, typeof inner, typeof a, typeof b, typeof c);
});
function outer() {
test("-- in outer, before a ---", function() {
testScope(typeof outer, typeof inner, typeof a, typeof b, typeof c);
});
var a = 1;
test("--- in outer, after a ---", function() {
testScope(typeof outer, typeof inner, typeof a, typeof b, typeof c);
});
function inner() {}
var b = 2;
test("--- in outer, after inner() and b ---", function() {
testScope(typeof outer, typeof inner, typeof a, typeof b, typeof c);
});
if (a == 1) {
var c = 3;
test("--- in outer, in if block ---", function() {
testScope(typeof outer, typeof inner, typeof a, typeof b, typeof c);
});
}
test("--- in outer, after c ---", function() {
testScope(typeof outer, typeof inner, typeof a, typeof b, typeof c);
});
}
outer();
test("--- after outer ---", function() {
testScope(typeof outer, typeof inner, typeof a, typeof b, typeof c);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment